Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 571
ranjitrock Avatar asked Sep 14 '11 12:09

ranjitrock


People also ask

How do I start a service with adb command?

Install ADB on Ubuntu For installing Android Debug Bridge (adb) command on Ubuntu, type below command on terminal $ sudo apt-get install adb Above command will also install android-libadb android-libbase android-libcutils android-liblog $ which adb /usr/bin/adb You can now check the adb version using command as below, ...

How can I shutdown my Android phone using an adb command?

You can shut down Android using the ADB command (adb shell reboot -p) on your PC, but you need to enable USB debugging on Android and install ADB drivers on your PC.

How do I start and stop adb?

To stop the adb server, use the adb kill-server command. You can then restart the server by issuing any other adb command.

How do I terminate adb shell?

To exit adb and return to the system shell use the $q or $Q command. To stop the debugger press <Ctrl>D. NOTE: adb cannot be stopped by pressing the Quit or Interrupt key.


2 Answers

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

like image 178
n611x007 Avatar answered Sep 18 '22 15:09

n611x007


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.

like image 20
bonnyz Avatar answered Sep 18 '22 15:09

bonnyz