Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,779,227 Members 47,121 Now Online
XDA Developers Android and Mobile Development Forum

Service terminates when app terminates

Tip us?
 
aeppacher
Old
#1  
aeppacher's Avatar
Senior Member - OP
Thanks Meter 688
Posts: 601
Join Date: Apr 2012
Default 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.

Code:
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:

Code:
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 tried adding

Code:
android:isolatedProcess="true"

to the manifest but it didn't help. I tried a few things that were recommended over here but so far no dice
Dalvik vs ART? Go here, don't post in Rom/Kernel threads.
 
painlessDeath
Old
#2  
Junior Member
Thanks Meter 2
Posts: 13
Join Date: Feb 2013
Check this article, it also suggests a potential solution
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/


And this open issue
https://code.google.com/p/android/issues/detail?id=63793
The Following User Says Thank You to painlessDeath For This Useful Post: [ Click to Expand ]
 
aeppacher
Old
#3  
aeppacher's Avatar
Senior Member - OP
Thanks Meter 688
Posts: 601
Join Date: Apr 2012
Quote:
Originally Posted by painlessDeath View Post
Check this article, it also suggests a potential solution
http://www.androidpolice.com/2014/03...is-on-the-way/


And this open issue
https://code.google.com/p/android/is...etail?id=63793
You are a god send. Tested the app on JB and the service stayed when I closed the app. Thank god.
Dalvik vs ART? Go here, don't post in Rom/Kernel threads.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes