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.