Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Stop a Service in Android when the application is not running.

Tags:

android

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

like image 353
Arun Kumar Avatar asked Sep 30 '11 11:09

Arun Kumar


People also ask

How do I stop a running service in Android?

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.

How to manually stop apps on Android devices?

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.

How to stop a service when app is killed?

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...

How do I stop an app from crashing on Android?

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.


2 Answers

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;
}
like image 157
Rocker Avatar answered Sep 30 '22 20:09

Rocker


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));
like image 23
Avinash Shinde Avatar answered Sep 30 '22 19:09

Avinash Shinde