How to start/stop a service from adb in Android

This article will introduce how to start or stop a service from adb in Android.

1. Start a service by "adb shell am start-service"

The adb shell am start-service <Intent> command can be used to launch a service for the given Intent.

Intents can be defined as an option in a command. For more detailed settings, please refer to the "Reference: Intent Setting Options" at the bottom of the article.

1.1 start a service in the foreground

You can run the service in the foreground with the following command.

$ adb shell am start-foreground-service <Intent>

2. Stop a service by "adb shell am stop-service"

The following command will stop the running service.

$ adb shell am stop-service <Intent>

3. Example: Start an Intent with only Action

If you define an intent with the -a option after the adb shell am start command, an intent with only the Action will be started.

$ adb shell am start-service -a android.intent.action.MY_ACTION

Output:

Starting service: Intent { act=android.intent.action.MY_ACTION }

4. Example: Start an Intent with Action and Category

You can add a Category using the -c option.

If there are two categories, you can enter them multiple times like -c AAA -c BBB.

$ adb shell am start-service -a android.intent.action.MY_ACTION -c android.intent.category.MY_CATEGORY

5. Example: Start an Intent with Action and Data

If you input a Uri with the -d option, an Intent with the data of the Uri will be executed.

$ adb shell am start-service -a android.intent.action.MY_ACTION -d https://google.com

6. Example: Start an Intent with Action and Package

You can start an Intent with the -p option set to the Package.

$ adb shell am start-service -a android.intent.action.MY_ACTION -p com.example.myapp

7. Example: Start an Intent with Action, Package and Component

You can start an Intent with the -n option set for the Package and Component.

$ adb shell am start-service -a android.intent.action.MY_ACTION -n com.example.myapp/com.example.myapp.MyService

8. Reference: Intent Setting Options

This is an option used when defining an intent from ADB command.

<INTENT> specifications include these flags and arguments:
    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>] [-i <IDENTIFIER>]
    [-c <CATEGORY> [-c <CATEGORY>] ...]
    [-n <COMPONENT_NAME>]
    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
    [--esn <EXTRA_KEY> ...]
    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
    [--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
    [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
    [--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
    [--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
    [--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
    [--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
        (mutiple extras passed as Integer[])
    [--eial <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
        (mutiple extras passed as List<Integer>)
    [--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
        (mutiple extras passed as Long[])
    [--elal <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
        (mutiple extras passed as List<Long>)
    [--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
        (mutiple extras passed as Float[])
    [--efal <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
        (mutiple extras passed as List<Float>)
    [--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
        (mutiple extras passed as String[]; to embed a comma into a string,
         escape it using "\,")
    [--esal <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
        (mutiple extras passed as List<String>; to embed a comma into a string,
         escape it using "\,")

Related Posts

codechachaCopyright ©2019 codechacha