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

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

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

相关标签:
11条回答
  • 2020-12-07 16:58

    am startservice <INTENT>   

    or actually from the OS shell

    adb shell am startservice <INTENT>

    0 讨论(0)
  • 2020-12-07 17:06

    You may get an error "*Error: app is in background *" while using

    adb shell am startservice 
    

    in Oreo (26+). This requires services in the foreground. Use the following.

    adb shell am start-foreground-service com.some.package.name/.YourServiceSubClassName
    
    0 讨论(0)
  • 2020-12-07 17:08

    To stop a service, you have to find service name using:

    adb shell dumpsys activity services <your package>
    

    for example: adb shell dumpsys activity services com.xyz.something

    This will list services running for your package.
    Output should be similar to:

    ServiceRecord{xxxxx u0 com.xyz.something.beta/xyz.something.abc.XYZService}
    

    Now select your service and run:

    adb shell am stopservice <service_name> 
    

    For example:

    adb shell am stopservice com.xyz.something.beta/xyz.something.abc.XYZService
    

    similarly, to start service:

    adb shell am startservice <service_name>
    

    To access service, your service(in AndroidManifest.xml) should set exported="true"

    <!-- Service declared in manifest -->
    <service
        android:name=".YourServiceName"
        android:exported="true"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="com.your.package.name.YourServiceName"/>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </service>
    
    0 讨论(0)
  • 2020-12-07 17:10

    You need to add android:exported="true" to start service from ADB command line. Then your manifest looks something like this:

    <!-- Service declared in manifest -->
    <service
        android:name=".YourServiceName"
        android:exported="true"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="com.your.package.name.YourServiceName"/>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </service> <!-- Note: Service is exported to start it using ADB command -->
    

    And then from ADB

    To start service:

    adb shell am startservice com.your.package.name/.YourServiceName

    To stop service (on Marshmallow):

    adb shell am stopservice com.your.package.name/.YourServiceName

    To stop service (on Jelly Bean):

    adb shell am force-stop com.your.package.name

    0 讨论(0)
  • 2020-12-07 17:13

    I can start service through

    am startservice com.xxx/.service.XXXService
    

    but i don't know how to stop it yet.

    0 讨论(0)
  • 2020-12-07 17:14

    If you want to run the script in adb shell, then I am trying to do the same, but with an application. I think you can use "am start" command

    usage: am [subcommand] [options]

    start an Activity: am start [-D] [-W] <INTENT>
        -D: enable debugging
        -W: wait for launch to complete
    
    **start a Service: am startservice <INTENT>**
    
    send a broadcast Intent: am broadcast <INTENT>
    
    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning
    
    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop
    
    start monitoring: am monitor [--gdb <port>]
        --gdb: start gdbserv on the given port at crash/ANR
    
    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--esn <EXTRA_KEY> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>]
        [--grant-read-uri-permission] [--grant-write-uri-permission]
        [--debug-log-resolution]
        [--activity-brought-to-front] [--activity-clear-top]
        [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
        [--activity-launched-from-history] [--activity-multiple-task]
        [--activity-no-animation] [--activity-no-history]
        [--activity-no-user-action] [--activity-previous-is-top]
        [--activity-reorder-to-front] [--activity-reset-task-if-needed]
        [--activity-single-top]
        [--receiver-registered-only] [--receiver-replace-pending]
        [<URI>]
    
    0 讨论(0)
提交回复
热议问题