Android Context.startForegroundService()随后未调用Service.startForeground

Android Context.startForegroundService()随后未调用Service.startForeground,android,foreground-service,Android,Foreground Service,我的应用程序将在main活动的onCreate中调用startForegroundService(intent)。我在服务的onCreate和startCommand中都放置了startForeground(在\u SERVICE\u CONNECTION\u NID上,通知)。 但我仍然偶尔收到这个错误 Exception android.app.RemoteServiceException: Context.startForegroundService() did not then call

我的应用程序将在
main活动的
onCreate
中调用
startForegroundService(intent)
。我在
服务的
onCreate
startCommand
中都放置了
startForeground(在\u SERVICE\u CONNECTION\u NID上,通知)
。 但我仍然偶尔收到这个错误

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

我不知道为什么每个人都说这和这个问题是一样的: 你甚至可以在那里找到我的评论

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

以下是谷歌问题追踪者:

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

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

我也面临同样的问题,在花时间找到解决方案后,您可以尝试下面的代码。如果您使用的是
服务
,则将此代码放入
onCreate
中;如果您使用的是
意向服务
,则将此代码放入
OnHandleContent

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));
    }

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

我把它放在应用程序类中:

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());
    } 

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

在尝试了不同的方法后,我发现其中一种方法适合我,如下所示:

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

我猜我出现这个问题的原因是应用程序启动的繁重工作负载延迟了前台服务的创建


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

你能在这里发布一些代码吗?我想用纯文本解释这个问题已经足够清楚了。你能告诉我你想知道什么吗?所以我可以添加更多的信息。看这个答案确切的问题是here@Anup不一样。我已经在你的链接和@UltimateDevil's链接中对答案进行了评论。我已经把
startForegroundService
startForeground
放在了我在问题中描述的正确位置。@swooby谢谢!实际上,我已经创建了另一个线程。那家也关门了。即使我提供了他们需要的所有信息和附件。他们说它“按预期工作”。然后,我发现
startForegroundService(intent)
不应该在
onCreate
活动中。因此,我将其移动到
onPause
,它仅在活动处于后台状态后将服务提升到前台。而
startForeground(在服务连接上,通知)
只能在服务的
onCreate
内。最后,错误报告停止。考虑使用<代码>