使用背景服务时在某些设备中崩溃

发布于 2025-01-17 18:37:26 字数 2396 浏览 0 评论 0原文

我使用了Flutter背景服务插件。有时我崩溃了。 我不知道为什么会发生?有任何解决方案吗? 我以多种方式尝试了。这很重要。 我使用Flutter_background_service 1.0.4作为背景服务以及前景服务。

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not 
then call Service.startForeground(): ServiceRecord{67fd813 u0 
com.example.sms_scheduler/id.flutter.flutter_background_service.BackgroundService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:6651)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

这是我的代码!

void main() async{
await initializeService();
  runApp(const MyApp());
}
Future<void> initializeService() async {
    final service = FlutterBackgroundService();
    await service.configure(
      androidConfiguration: AndroidConfiguration(
        onStart: onStart,
        autoStart: true,
        isForegroundMode: true,
      ),
      iosConfiguration: IosConfiguration(
        autoStart: true,
        onForeground: onStart,
        onBackground: onIosBackground,
      ),
    );
}
void onIosBackground() {
  WidgetsFlutterBinding.ensureInitialized();
  print('FLUTTER BACKGROUND FETCH');
}
void onStart() {
    if (Platform.isAndroid){PathProviderAndroid.registerWith();}
    else if (Platform.isIOS){PathProviderIOS.registerWith();}
    if (Platform.isIOS) FlutterBackgroundServiceIOS.registerWith();
    if (Platform.isAndroid) FlutterBackgroundServiceAndroid.registerWith();
    final service = FlutterBackgroundService();
    service.onDataReceived.listen((event) {
        if (event!["action"] == "setAsForeground") {
          service.setAsForegroundService();
          return;
        }
        if (event["action"] == "setAsBackground") {
          service.setAsBackgroundService();
        }
        if (event["action"] == "stopService") {
          service.stopService();
        }
      });
    service.setAsBackgroundService();
    Timer.periodic(const Duration(seconds: 05), (timer) async {
      service.sendData(
          {
            "final_sms": sendFinalSMS(),
          }
      );
    });
}

I used flutter background service plug-in.Sometimes I getting crash.
I don't know why it is happening? is there any solution??
I tried it by many ways..It's very critical..
I am using flutter_background_service 1.0.4 as background services as well as foreground services.

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not 
then call Service.startForeground(): ServiceRecord{67fd813 u0 
com.example.sms_scheduler/id.flutter.flutter_background_service.BackgroundService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:6651)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

This is my Code !!

void main() async{
await initializeService();
  runApp(const MyApp());
}
Future<void> initializeService() async {
    final service = FlutterBackgroundService();
    await service.configure(
      androidConfiguration: AndroidConfiguration(
        onStart: onStart,
        autoStart: true,
        isForegroundMode: true,
      ),
      iosConfiguration: IosConfiguration(
        autoStart: true,
        onForeground: onStart,
        onBackground: onIosBackground,
      ),
    );
}
void onIosBackground() {
  WidgetsFlutterBinding.ensureInitialized();
  print('FLUTTER BACKGROUND FETCH');
}
void onStart() {
    if (Platform.isAndroid){PathProviderAndroid.registerWith();}
    else if (Platform.isIOS){PathProviderIOS.registerWith();}
    if (Platform.isIOS) FlutterBackgroundServiceIOS.registerWith();
    if (Platform.isAndroid) FlutterBackgroundServiceAndroid.registerWith();
    final service = FlutterBackgroundService();
    service.onDataReceived.listen((event) {
        if (event!["action"] == "setAsForeground") {
          service.setAsForegroundService();
          return;
        }
        if (event["action"] == "setAsBackground") {
          service.setAsBackgroundService();
        }
        if (event["action"] == "stopService") {
          service.stopService();
        }
      });
    service.setAsBackgroundService();
    Timer.periodic(const Duration(seconds: 05), (timer) async {
      service.sendData(
          {
            "final_sms": sendFinalSMS(),
          }
      );
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-01-24 18:37:26

你为什么不使用这个

尝试{
//可能会引发异常的代码
} catch(exception_class_name ref){}

Why don't you use this

try{
//code that may throw an exception
}catch(Exception_class_Name ref){}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文