4

App and background service are running together.But when app closed,background service is restarting?
Why? How can I do block this restarting?

When the application is closed, the service should continue like nothing happened.How can I do that?



I use this codes in MainActivity:

tryConnect();

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
private void tryConnect(){
    if(!isMyServiceRunning(BackService.class)){
        startService(new Intent(this,BackService.class));
    }
}

Background Service return START_STICKY; (and I tried START_NOT_STICKY,START_REDELIVER_INTENT,doesn't work)
I dont have a onDestroy in android app.

erginduran
  • 1,678
  • 5
  • 28
  • 51
  • The OS can stop your service at anytime when it requires some memory. So , better to make a service which will not be affected by any of those issues – Nish8900 Aug 27 '14 at 09:12

1 Answers1

0

Are the service and the acticity running on the same process? If so then that might be why your service is behaving like this (if both share the same process then when the app dies to process dies too causing the service to restart itself if it uses START_STICKY)... Maybe you should try running the service in a seperate process..

Sahar Weissman
  • 162
  • 1
  • 4