How to start an activity from adb in Android

This article will introduce how to start an activity from adb command in Android.

1. Start an activity by "adb shell am start"

The adb shell am start <Intent> command can be used to launch an activity 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.

2. 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 -a android.intent.action.MY_ACTION

Output:

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

3. 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 -a android.intent.action.MY_ACTION -c android.intent.category.MY_CATEGORY

4. 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 -a android.intent.action.MAIN -d https://google.com

5. 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 -a android.intent.action.MAIN -p com.example.myapp

6. 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 -a android.intent.action.MAIN -n com.example.myapp/com.example.myapp.MainActivity

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