我从设备监视器
中得到以下异常输出:
//FATAL EXCEPTION: main
//Process: com.xxx.yyy, PID: 11584
//android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
// at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
// at android.os.Handler.dispatchMessage(Handler.java:105)
// at android.os.Looper.loop(Looper.java:164)
// at android.app.ActivityThread.main(ActivityThread.java:6944)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
我的应用程序中有4个服务,所有这些服务基本上都是这样的:
public class MyService extends Service {
private static final int forgroundId = 55544433; //Unique for each Service
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initNotification(); //Creates a notification
startForeground(forgroundId, notification); //Start foreground, very first thing..
//Do Stuff
return START_STICKY;
}
}
启动服务时,我使用以下命令:
Intent i = new Intent(context, MyService.class);
i.SetAction("myaction..");
i.PutExtra(...);
android.support.v4.content.ContextCompat.startForegroundService(context, i);
我在我的项目中还包含了一些可能启动服务的其他库:
- Fabric.io
- Firebase Messaging/Analytics等。
- Google Play服务的分析/GCM/Tasks等。
- Evernote的Android-Job
有没有任何方法可以[捕获/抛出/我不知道]异常来确定异常的来源?
我已经阅读了所有以下帖子,但都没有运气: 这种情况只会在打开应用程序时偶尔发生,因此对我来说我的代码是正确的?
我只想知道如何找出这是来自哪个服务。
WorkManager
(或Android-Job,因为您似乎已经在使用它),而不是为此编写自己的服务。 - CommonsWareonDestroy
和onLowMemory
方法解决了我的问题。 - Pierre