Android Alarm Manager崩溃Flutter应用程序-运行时异常,线程/异步问题

时间:2019-07-13 22:52:01

标签: android flutter dart alarmmanager

呼叫时:

await AndroidAlarmManager.oneShot(Duration(seconds: 1), 17, callback)

在我的计时器类中,具有如下回调函数:

  static void alarmCallback(){
    print('>>>>>>>>>>>>>>>>>>>> ALARM CALLBACK <<<<<<<<<<<<<<<<< ');
  }

我遇到错误:

E/AndroidRuntime( 9115): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime( 9115): Process: com.myapp, PID: 9115
E/AndroidRuntime( 9115): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime( 9115):    at android.os.AsyncTask$3.done(AsyncTask.java:354)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime( 9115):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 9115):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 9115):    at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime( 9115): Caused by: java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: AsyncTask #2
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:684)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:80)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:189)
E/AndroidRuntime( 9115):    at io.flutter.view.FlutterNativeView.send(FlutterNativeView.java:155)
E/AndroidRuntime( 9115):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:98)
E/AndroidRuntime( 9115):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:84)
E/AndroidRuntime( 9115):    at io.flutter.plugins.androidalarmmanager.AlarmService.invokeCallbackDispatcher(AlarmService.java:170)
E/AndroidRuntime( 9115):    at io.flutter.plugins.androidalarmmanager.AlarmService.onHandleWork(AlarmService.java:151)
E/AndroidRuntime( 9115):    at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:392)
E/AndroidRuntime( 9115):    at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:383)
E/AndroidRuntime( 9115):    at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime( 9115):    ... 3 more
D/ViewRootImpl@c3ee092[MainActivity]( 9115): MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager( 9115): prepareNavigationBarInfo() DecorView@360043a[MainActivity]
D/InputMethodManager( 9115): getNavigationBarColor() -855310
I/Process ( 9115): Sending signal. PID: 9115 SIG: 9
Lost connection to device.

我按照https://pub.dev/packages/android_alarm_manager

中的官方文档实现了android_alarm_manager
  • 添加了权限,服务并显示了清单
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
        <service
            android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
  • 等待AndroidAlarmManager.initialize();在应用午餐时被调用
  • 使用新类(与清单中的应用程序名称相同)完成PluginRegistrant
<application
        android:name=".Application"
public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        AlarmService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

警报已经初始化-在调用oneShot()之前:

I/AlarmService( 9115): Starting AlarmService...
I/AlarmService( 9115): AlarmService started!

我已经在寻找该错误,例如在这里:

Flutter Android Alarm Manager not working

或此处:

Android Alarm Manager is not working for Flutter Project App

0 个答案:

没有答案