I have Started my service in one of my main activity class.The problem is that when my application is not running I want to Stop my service.How can i implement this.Is there any option to stop it without stopping it using any button click or other click events of the control.
Also is there any way to indicate the service is running or not while my application is running
Call stopSerivce (intent) method to stop the service. If you want to make sure the background service is running after you start it, you can go to Settings —> Developer options —> Running services to see it. If you want to see the running andriod service in a terminal, you can run the below shell command to list all the running android services.
Please tap on settings > apps > select tab Running and you see all the apps. Tap one app that you plan to manually stop. On the next screen you see STOP button to stop that apps. Uninstall unnecessary Android applications.
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service ? inside onstart command put START_STICKY...
Manually Stop Android Running Apps. You may manually stop the apps before uninstalling it and see it is making some crashes in your Android OS or the services that you required. Please tap on settings > apps > select tab Running and you see all the apps. Tap one app that you plan to manually stop.
Simply by firing an Intent like this:
Intent intent = new Intent(this, Your_service.class);
Just add this block of code where you have stop your service:
stopService(intent);
Update:
add this method to each and every activity of your application
public boolean onKeyDown(int key, KeyEvent event)
{
switch(key.getAction())
{
case KeyEvent.KEYCODE_HOME :
Intent intent = new Intent(this, Your_service.class);
stopService(intent);
break;
}
return true;
}
Using intent you can stop the service.
Intent intentStartervice = new Intent(context, MyService.class);
stopService(intentStartService);
or
just pass the Context.
context.stopService(new Intent(context, MyService.class));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With