I dont stop my Service
, it is STICKY
Service
. When starting app i am controlling the service is running or not like :
public static boolean isMyServiceRunning(Class<?> serviceClass,Context mContext) {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Usage :
if(!isMyServiceRunning(MyService.class, getApplicationContext())){
startService(new Intent(this, MyService.class));
}
But when i logout my user i am stopping service like :
Intent serviceIntent=new Intent(mContext,MyService.class);
mContext.getApplicationContext().stopService(serviceIntent);