我的应用程序将在MainActivity
的onCreate
中调用startForegroundService(intent)
。我把startForeground(ON_SERVICE_CONNECTION_NID, notification)
放在了onCreate
和Service
的startCommand
中。但是我仍然偶尔会收到这个错误。
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秒内没有调用onCreate
或startCommand
?如果是这样,我们应该如何调用startForegroundService
而不出错呢?
更新2017-12-09
我不知道为什么每个人都说这和这个问题是一样的:Context.startForegroundService() did not then call Service.startForeground(),你甚至可以在那里找到我的评论。
它之所以不同,是因为你可以在这个问题的第一行看到。我已经将startForegroundService
和startForeground
放在了正确的位置,但是它仍然随机地显示这个错误。
这是谷歌的问题跟踪器:https://issuetracker.google.com/issues/67920140
在此之前停止服务将导致崩溃。
这是关于服务生命周期的另一个问题。服务关闭后,断言可能会被错误地触发。
发布于 2018-04-09 15:24:10
我正面临着同样的问题,在花费时间找到解决方案后,你可以尝试下面的代码。如果您使用的是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));
}
发布于 2018-04-25 19:36:20
我也有同样的问题。
最后,对我有帮助的是尽可能早地初始化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());
}
发布于 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
唤醒了接收器,在接收器中,我启动了前台服务。
我猜我有这个问题的原因是因为应用程序启动的繁重工作延迟了前台服务的创建。
尝试我的方法,你也可以解决这个问题。祝好运。
https://stackoverflow.com/questions/46803663
复制相似问题