How to start and stop android service from a adb shell?
I need to write a shell script to start and stop an android service .
Solution 1:
I'm a beginner in Android, but got it working like this:
in AndroidManifest.xml, make sure you, inside <application>
, have something like this:
<service android:name="com.some.package.name.YourServiceSubClassName" android:permission="com.some.package.name.YourServiceSubClassName">
<intent-filter>
<action android:name="com.some.package.name.YourServiceSubClassName"/>
</intent-filter>
</service>
where YourServiceSubClassName
extend android.app.Service
is your java class that is the service. Where com.some.package
is the package name, for me both in AndroidManifest.xml and in Java.
Used a javabeat.net article as help, look for <service>
Note also, supposedly between the package name and the class name there should be .service.
in the text, I guess this is some convention, but for me this caused ClassNotFoundException
that I'm yet to solve.
Then, install your apk. I did from eclipse but also adb install -r yourApkHere.apk
should work. Uninstall is adb uninstall com.some.package.name
, btw.
You can start it from host system like this, thanks Just a Tim and MrRoy:
adb shell am startservice com.some.package.name/.YourServiceSubClassName
interestingly, I didn't need -n
.
To stop, I use
adb shell am force-stop com.some.package.name
Hope it helps.
As I'm a beginner, please feel freet to edit/comment to fix any misconceptions (eg. probably regarding .service.
in the component (?) name).
Solution 2:
Starting a service:
adb shell am startservice ...
start a Service. Options are: --user | current: Specify which user to run as; if not specified then run as the current user.
Stopping a service:
adb shell am stopservice ...
stop a Service. Options are: --user | current: Specify which user to run as; if not specified then run as the current user.