How To Stop Running Services?
I have this code that describes a service: public class navigation_web extends Service { Context context; public void navigation() { Cursor mCur = getContentResol
Solution 1:
There is another method that it's called stopService(Intent intent)
.
Inside the service, you can call stopSelf()
.
Solution 2:
use the activity stopService() method:
stopService(newIntent(ActivityName.this, ServiceClassName.class));
Solution 3:
Solution 4:
android.os.Process.killProcess(android.os.Process.myPid());
Solution 5:
Complementing @Makvin answer. Stopping a service outside of it
publicstatic ActivityManager.RunningServiceInfo getRunningServiceInfo(Class serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return service;
}
}
returnnull;
}
And after
newTimer().schedule(newTimerTask() {
@Overridepublicvoidrun() {
final ActivityManager.RunningServiceInfoserviceInfo= getRunningServiceInfo(service, c);
if (serviceInfo != null) {
android.os.Process.killProcess(serviceInfo.pid); //Stop the running service
}
}
}, 200);
I'm putting killProcess inside another thread otherwise for some reason all the app is killed. In this way the app is minimized when the process is killed (less bad), suggestions are welcome
Post a Comment for "How To Stop Running Services?"