首页 文章

然后Context.startForegroundService()没有调用Service.startForeground

提问于
浏览
4

我的应用程序将在 MainActivityonCreate 中调用 startForegroundService(intent) . 我把 startForeground(ON_SERVICE_CONNECTION_NID, notification) 放在了 onCreateServiceService 中 . 但我偶尔会收到这个错误 .

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-09

我和这个问题一样't know why every one said that it':你甚至可以在那里找到我的评论 .

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

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

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

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

2 回答

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

相关问题