3

i have the following onCreate() on the MainActivity:

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

and this Service:

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;
    }

}

When, form the Android task manager i kill only the BackgroundService (from "In Use" tab), or from the "recent app" i swipe out my application, the the service just restart, and this is ok.

But.. when from the tab "All Apps" i kill the App itself the service does not restart properly.

Using Logcat via ADB i can see the following logs:

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

I can't understand why after the

Scheduling restart of crashed service

there are these two lines:

Force finishing activity Force stopping service

This is the manifest part about the service:

<service
   android:name=".BackgroundService"
   android:enabled="true"
   android:exported="true"
   android:stopWithTask="false">
</service>
Matt
  • 721
  • 9
  • 26

0 Answers0