How to send/broadcast an intent from adb command in Android

This article will introduce how to send an intent from adb command in Android.

1. "adb shell am broadcast" command

You can use the adb shell am broadcast <Intent> command to broadcast an Intent.

Intent can be defined as an option in the command. For detailed setup instructions, please refer to the "Reference: Intent Setup Options" at the bottom of the article.

2. Send an Intent with only an Action

if you enter an Action name like -a android.intent.action.MY_ACTION in the -a option, the Intent with that Action will be broadcasted.

$ adb shell am broadcast -a android.intent.action.MY_ACTION

Output:

Broadcasting: Intent { act=android.intent.action.MY_ACTION flg=0x400000 }
Broadcast completed: result=0

3. Send an Intent with an Action and Category.

If you enter the name of the Category in the -c option, you can set the Category in the intent.

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

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

4. Send an Intent with Action and Data

If you enter a Uri in the -d option, an Intent with the data such as the Uri scheme will be delivered.

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

4. Send an Intent with package name

An Intent with the Package set with the -p option is delivered. Because the package is set, the broadcast is only delivered to that package.

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

5. Reference: Intent Setup Options

This is an option used when passing an intent to an 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