简体   繁体   中英

Android service terminates when app terminates

I am currently trying to implement an app that has a service running until the user explicitly ends it via the app. I would like the service to remain on otherwise. My current problem is that whenever the app is removed from the recent apps, it terminates the service as well. I have tried using START_STICKY in my onStartCommand but it doesn't change anything.

public class TriggerService extends Service{

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

and here is my code for when I call the service:

public void startServ(boolean state){
    editor = sp.edit();

    if (state == true) {
        startService(new Intent(currentActivity, TriggerService.class));
        editor.putBoolean("service_status", true);
        toast = Toast.makeText(currentActivity, "Service Running", Toast.LENGTH_SHORT);
        toast.show();
    } else {
        stopService(new Intent(currentActivity, TriggerService.class));
        editor.putBoolean("service_status", false);
        toast = Toast.makeText(currentActivity, "Service Not Running", Toast.LENGTH_SHORT);
        toast.show();
    }

    editor.commit();
}

EDIT: I added

android:isolatedProcess="true"

to the androidmanifest for the service as suggested but the service terminates when the app does.

When a user swipes an app out of the recent history list, the process is terminated. If you start a service inside the same process as your activity, that service will be terminated when the user swipes the app out.

The guide mentions starting services in different processes by simply setting a flag in the manifest but that will require you to use AIDL to move data between the service and the app or move the data indirectly via intents.

http://developer.android.com/guide/topics/manifest/service-element.html

<service android:enabled=["true" | "false"]
         android:exported=["true" | "false"]
         android:icon="drawable resource"
         android:isolatedProcess=["true" | "false"]
         android:label="string resource"
         android:name="string"
         android:permission="string"
         android:process="string" >
    . . .
</service>

Found the problem. The above code will work. The problem is there is a bug in the code for 4.4.2 which terminates services related to apps when you terminate those apps. An example of the issue is here http://www.androidpolice.com/2014/03/07/bug-watch-stopping-apps-on-android-4-4-2-can-silently-kill-related-background-services-a-fix-is-on-the-way/ with the bug tracker here https://code.google.com/p/android/issues/detail?id=63793

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM