杀死后ActivityManager无法重启服务

时间:2016-10-17 10:43:23

标签: android service background activity-manager

我在MainActivity上有以下onCreate():

public void onCreate(Bundle savedInstanceState) {
    Intent intent = new Intent(this, BackgroundService.class);
    getApplicationContext().startService(intent);
}

和本服务:

public class BackgroundService extends Service {

    private Looper mServiceLooper;
    private ServiceHandler mServiceHandler;

    public BackgroundService() {}

    @Override
    public void onCreate() {
        HandlerThread thread = new HandlerThread("ServiceStartArguments");
        thread.start();
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    private final class ServiceHandler extends Handler {

        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            try {

                while(true) {
                    Log.d("BackgroundService", "Hi! I'm working...");
                    Thread.sleep(10000);
                }

            } catch (Exception e) {
                Thread.currentThread().interrupt();
            }

            stopSelf(msg.arg1);
        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        mServiceHandler.sendMessage(msg);

        return START_STICKY;

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

当形成Android任务管理器时,我只杀死BackgroundService(来自“使用中”选项卡),或者从“最近的应用程序”中删除我的应用程序,该服务只是重启,这没关系。

但是..当从“所有应用程序”选项卡中删除应用程序本身时,服务无法正常重启。

通过ADB使用Logcat我可以看到以下日志:

I/ActivityManager(  557): Force stopping io.fornace.giarvis appid=10185 user=0: from pid 21521
I/ActivityManager(  557): Killing 31260:io.fornace.giarvis/u0a185 (adj 9): stop io.fornace.giarvis
D/ConnectivityService(  557): ConnectivityService NetworkRequestInfo binderDied(NetworkRequest [ id=301, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ], android.os.BinderProxy@c24d6f5)
I/WindowState(  557): WIN DEATH: Window{1b01ccf2 u0 io.fornace.giarvis/io.fornace.giarvis.MainActivity}
D/ConnectivityService(  557): releasing NetworkRequest NetworkRequest [ id=301, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
E/ConnectivityService(  557): RemoteException caught trying to send a callback msg for NetworkRequest [ id=301, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
W/ActivityManager(  557): Scheduling restart of crashed service io.fornace.giarvis/.BackgroundService in 1000ms
I/ActivityManager(  557):   Force finishing activity 3 ActivityRecord{adf055c u0 io.fornace.giarvis/.MainActivity t281}
I/ActivityManager(  557):   Force stopping service ServiceRecord{3a1c7d8c u0 io.fornace.giarvis/.BackgroundService}
W/ActivityManager(  557): Spurious death for ProcessRecord{1bac5c8a 31260:io.fornace.giarvis/u0a185}, curProc for 31260: null

我无法理解

后的原因
  

调度崩溃服务的重启

有这两行:

  

强制完成活动   强制停止服务

这是该服务的明显部分:

<service
   android:name=".BackgroundService"
   android:enabled="true"
   android:exported="true"
   android:stopWithTask="false">
</service>

0 个答案:

没有答案