How to start and stop android service from a adb shell?

前端 未结 11 1065
别跟我提以往
别跟我提以往 2020-12-07 16:32

I need to write a shell script to start and stop an android service .

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 17:25

    I'm a beginner in Android, but got it working like this:

    in AndroidManifest.xml, make sure you, inside , have something like this:

    
        
            
        
    
    

    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

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

提交回复
热议问题