首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >然后,Context.startForegroundService()没有调用Service.startForeground

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

Stack Overflow用户
提问于 2017-10-18 13:57:50
回答 3查看 3.4K关注 0票数 4

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

代码语言:javascript
复制
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)

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

更新2017-12-09

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

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

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

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

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

EN

回答 3

Stack Overflow用户

发布于 2018-04-09 15:24:10

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

代码语言:javascript
复制
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);
}

把这个叫作

代码语言:javascript
复制
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(getSyncIntent(context));
    } else {
        context.startService(getSyncIntent(context));
    }
票数 0
EN

Stack Overflow用户

发布于 2018-04-25 19:36:20

我也有同样的问题。

最后,对我有帮助的是尽可能早地初始化NotificationChannel (我在应用程序类本身中完成了),然后在服务中创建和使用通知。

我把这个放在Application类中:

代码语言:javascript
复制
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方法中:

代码语言:javascript
复制
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        startForeground(NOTIFICATION_ID, createNotification());
    } 
票数 0
EN

Stack Overflow用户

发布于 2019-05-24 02:14:04

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

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

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46803663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档