Reputation: 877
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
Upvotes: 5
Views: 22228
Reputation: 8240
You can use Activity manager on the adb shell:
adb shell dumpsys activity services [text search]
Replace [text search]
with some text of your service to look for. You should get a "service record" with the following qualified name for the service:
/<fully.qualified.serviceclass>
Then stop it as so:
adb shell am stop-service <package name>/<fully.qualified.serviceclass>
Note that the service must have exported=true
in the AndroidManifest.xml
, otherwise you'll get an
Error stopping service
When running this.
Upvotes: 0
Reputation: 385
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));
Upvotes: 4
Reputation: 681
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;
}
Upvotes: 10
Reputation: 19250
You can start service in onCreate of your launcher activity and stop service in onStop of the same activity. You can refer to this for complete service tutorial:
http://marakana.com/forums/android/examples/60.html
And yes,better you use the same intent variable to start and stop activity.At last,Don't forget to add your service to manifest.
Upvotes: 0