Context.startForegroundService()未调用Service.startForeground导致问题

4

我的应用程序在MainActivityonCreate中调用startForegroundService(intent)。我在ServiceonCreatestartCommand中都放置了startForeground(ON_SERVICE_CONNECTION_NID, notification),但有时仍然会收到错误。

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)

这是怎么发生的呢? onCreate或者startCommand在5秒内没有被调用,这有可能吗? 如果是这样的话,我们应该如何调用startForegroundService而不出现错误?
更新于2017-12-09
我不知道为什么每个人都说这和这个问题一样:Context.startForegroundService() did not then call Service.startForeground() 你甚至可以在那里找到我的评论。
它之所以不同,是因为你可以看到这个问题的第一行。我已经将startForegroundServicestartForeground放在了正确的位置,但它仍然会随机显示此错误。
这里是Google的问题跟踪器:https://issuetracker.google.com/issues/67920140

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

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

我认为用纯文本来解释问题已经足够清楚了。你能告诉我你想知道什么吗?这样我就可以添加更多的信息。 - Kimi Chiu
请查看此答案 https://dev59.com/AVcP5IYBdhLWcg3wa5bQ,原问题在此。 - UltimateDevil
@UltimateDevil,请在这里阅读我的问题,你会发现我已经在ServiceonCreate中使用了startForeground。只是让你知道,我已经在那里的“答案”下面进行了评论。 - Kimi Chiu
3
@Anup 这不一样。我已经在你提供的链接下面和 @UltimateDevil 的链接下面发表了评论。我已经按照我在问题中描述的那样将 startForegroundServicestartForeground 放在了正确的位置。 - Kimi Chiu
1
@swooby 谢谢!实际上,我已经创建了另一个线程。但那个也被关闭了。即使我提供了所有所需的信息和附件,他们仍然说它“按预期工作”。之后,我发现 startForegroundService(intent) 不应该在活动的 onCreate 中。所以我将其移动到 onPause 中,这样只有在 Activity 处于后台状态时才会将服务提升到前台。而 startForeground(ON_SERVICE_CONNECTION_NID, notification) 应该只在服务的 onCreate 中。最终错误报告停止了。 - Kimi Chiu
显示剩余4条评论
3个回答

0

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

尝试了不同的方法后,我找到了以下适合我的方法:

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

我猜测我遇到这个问题的原因是应用程序启动的重负载延迟了前台服务的创建。

尝试我的方法,你也可能解决这个问题。祝好运。


0
我曾经遇到过同样的问题。
最终帮助我的是尽早初始化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());
    } 

0

我面临同样的问题,花了一些时间找到了一个解决方案,你可以尝试下面的代码。如果你正在使用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));
    }

1
考虑使用 ContextCompat.startForegroundService() 代替。 - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接