gpt4 book ai didi

android - 如何触发 ActivityAware 覆盖方法 onAttachedToActivity()?

转载 作者:行者123 更新时间:2023-12-04 13:37:16 27 4
gpt4 key购买 nike

我需要引用我的插件的 Activity 。根据official docs ,我实际上需要实现ActivityAware,并获得对 Activity 的引用,它由onAttachedToActivity提供覆盖功能。如何触发此覆盖功能?我居然查了this doc但目前还不清楚如何触发它,因为我在任何地方都没有看到任何示例代码。它说

This method can be invoked in 1 of 2 situations:

This ActivityAware FlutterPlugin was just added to a FlutterEngine that was already connected to a running Activity. This ActivityAware FlutterPlugin was already added to a FlutterEngine and that FlutterEngine was just connected to an Activity.



我的代码如下所示:
public class MyPlugin implements FlutterPlugin, ActivityAware {
Activity activity;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
// TODO: your plugin is now attached to a Flutter experience.
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
// TODO: your plugin is no longer attached to a Flutter experience.
}
@Override
public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
activity = activityPluginBinding.getActivity()

// TODO: your plugin is now attached to an Activity
}

@Override
public void onDetachedFromActivityForConfigChanges() {
// TODO: the Activity your plugin was attached to was
// destroyed to change configuration.
// This call will be followed by onReattachedToActivityForConfigChanges().
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
// TODO: your plugin is now attached to a new Activity
// after a configuration change.
}

@Override
public void onDetachedFromActivity() {
// TODO: your plugin is no longer associated with an Activity.
// Clean up references.
}

最佳答案

我能够让它与以下缩写实现一起工作。您实现 ActivityWare接口(interface),然后通过 activityPluginBinding.activity 获取 Activity class ProPayFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activityPluginBinding = binding
initializeCardReaderDependencies(activityPluginBinding.activity)
}
    private fun initializeCardReaderDependencies(activity: Activity) {
Log.d(LOG_TAG, "Initializing card reader dependencies")
SDKManager.initialize(activity.application)
Terminal.initialize()
cardReaderController = CardReaderController.getControllerFor(BBPOSDevice::class.java)
val meaningfulMessageListener = CardReaderEventStream(cardReaderController)
cardReaderNotificationChannel.setStreamHandler(meaningfulMessageListener)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.startLockTask()
}
}

关于android - 如何触发 ActivityAware 覆盖方法 onAttachedToActivity()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61134326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com