【问题标题】:Context.startForegroundService() did not then call Service.startForegroundContext.startForegroundService() 然后没有调用 Service.startForeground
【发布时间】:2018-03-29 22:38:09
【问题描述】:

我的应用将在MainActivityonCreate 中调用startForegroundService(intent)。我将startForeground(ON_SERVICE_CONNECTION_NID, notification) 放在onCreateServicestartCommand 中。 但我仍然偶尔会收到此错误。

Exception android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1775)
android.os.Handler.dispatchMessage (Handler.java:105)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6541)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

怎么会这样? onCreatestartCommand 是否有可能在 5 秒内没有被调用? 如果是这样,我们应该如何调用startForegroundService而不出错?

2017 年 12 月 9 日更新

不知道为什么大家都说和这个问题一样:Context.startForegroundService() did not then call Service.startForeground() 你甚至可以在那边找到我的评论。

它不同的原因是你可以在这个问题的第一行看到。我已将startForegroundServicestartForeground 放在正确的位置,但它仍然随机显示此错误。

这是 Google 问题跟踪器:https://issuetracker.google.com/issues/67920140

在此之前停止服务会导致崩溃。

这是关于服务生命周期的另一个问题。 服务关闭后,断言可能会被错误触发。

【问题讨论】:

  • 你可以在这里发布一些代码
  • 我认为用纯文本解释问题已经足够清楚了。你能告诉我你想知道什么吗?所以我可以添加更多信息。
  • 看到这个答案stackoverflow.com/questions/44425584/… 确切的问题在这里
  • @Anup 不一样。我已经在您的链接和@UltimateDevil 的链接中评论了该答案。正如我在问题中描述的那样,我已经将 startForegroundServicestartForeground 放在了正确的位置。
  • @swooby 谢谢!实际上,我已经创建了另一个线程。那个也被关闭了。即使我提供了他们需要的所有信息和附件。他们说它“按预期工作”。之后,我发现startForegroundService(intent) 不应该在活动的onCreate 中。所以我把它移到onPause,它只在活动处于后台状态后将服务提升到前台。而startForeground(ON_SERVICE_CONNECTION_NID, notification) 应该只在服务的onCreate 内。最后报错停止。

标签: android foreground-service


【解决方案1】:

我也面临同样的问题,花时间找到解决方案后,您可以尝试以下代码。如果您使用Service,则将此代码放入onCreate,否则您使用Intent Service,则将此代码放入onHandleIntent

if (Build.VERSION.SDK_INT >= 26) {
    String CHANNEL_ID = "my_app";
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
            "MyApp", NotificationManager.IMPORTANCE_DEFAULT);
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("")
            .setContentText("").build();
    startForeground(1, notification);
}

并调用它

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(getSyncIntent(context));
    } else {
        context.startService(getSyncIntent(context));
    }

【讨论】:

  • 考虑改用ContextCompat.startForegroundService()
【解决方案2】:

我遇到了同样的问题。
最后帮助我的是尽早初始化 NotificationChannel(我是在应用程序类本身中完成的),然后在服务中创建和使用通知。

我把它放在 Application 类中:

private void createNotificationChannel() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence name = "MyStreamingApplication";
        String description = "radio";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setSound(null, null);
        mChannel.enableVibration(false);
        mChannel.setDescription(description);
        notificationManager.createNotificationChannel(mChannel);
    }
}

在我调用的服务的 onStartCommand 方法中:

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        startForeground(NOTIFICATION_ID, createNotification());
    } 

【讨论】:

    【解决方案3】:

    我在 Galaxy Tab A(Android 8.1.0)上遇到了这个问题。在我的应用程序中,我在MainApplication(应用程序扩展类)的构造函数中启动了一个前台服务。使用调试器,我发现在startForegroundService(intent) 之后到达服务的OnCreate() 需要超过 5 秒的时间。即使startForeground(ON_SERVICE_CONNECTION_NID, notification)OnCreate() 调用,我还是崩溃了。

    在尝试了不同的方法后,我发现一个适合我的方法如下:

    在构造函数中,我使用AlarmManager唤醒了一个receiver,在receiver中,我启动了前台服务。

    我猜我遇到问题的原因是因为应用程序启动的繁重工作量延迟了前台服务的创建。

    试试我的方法,你也可以解决问题。祝你好运。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2018-03-04
      • 2017-11-09
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 2019-09-17
      相关资源
      最近更新 更多