How to use the command 'am' (with examples)
- Android
- December 25, 2023
The am
command is the Android activity manager, which allows you to control various aspects of the Android system, such as starting activities, managing intents, and converting intents to URIs. It is a powerful tool for developers, as it enables them to interact with the Android system from the command line.
Use case 1: Start a specific activity
Code:
am start -n com.android.settings/.Settings
Motivation: Starting a specific activity is useful when you want to navigate directly to a particular screen or setting in an Android app. For example, if you want to open the Settings app, you can use the am start
command to launch the Settings activity.
Explanation:
-n com.android.settings/.Settings
: Specifies the component name of the activity to start. In this case, we are specifying the activitycom.android.settings/.Settings
, which corresponds to the Settings app.
Example output: The Settings app will be launched, and the user will be taken to the main screen of the app.
Use case 2: Start an activity and pass data to it
Code:
am start -a android.intent.action.VIEW -d tel:123
Motivation: Passing data to an activity is useful when you want to pre-fill form fields, provide context, or perform specific actions within an app. In this case, we are starting an activity with an intent to view a telephone number.
Explanation:
-a android.intent.action.VIEW
: Specifies the action of the intent, which is to view something.-d tel:123
: Specifies the data of the intent, which is a telephone number. Thetel:
scheme indicates that the data is a phone number.
Example output: The Android system will launch an activity that can handle viewing telephone numbers, and the number “123” will be passed to the activity.
Use case 3: Start an activity matching a specific action and category
Code:
am start -a android.intent.action.MAIN -c android.intent.category.HOME
Motivation: Matching a specific action and category is useful when you want to launch an activity that serves a particular purpose. In this case, we are starting the home screen activity of the Android device.
Explanation:
-a android.intent.action.MAIN
: Specifies the action of the intent, which is the main entry point of the app.-c android.intent.category.HOME
: Specifies the category of the intent, which indicates that the activity should be treated as the home screen.
Example output: The Android system will launch the default home screen activity of the device.
Use case 4: Convert an intent to a URI
Code:
am to-uri -a android.intent.action.VIEW -d tel:123
Motivation: Converting an intent to a URI is useful when you want to generate a unique identifier for an intent or share the intent with other apps. In this case, we are converting an intent to a URI that represents a telephone number.
Explanation:
to-uri
: Specifies that we want to convert the intent to a URI.-a android.intent.action.VIEW
: Specifies the action of the intent, which is to view something.-d tel:123
: Specifies the data of the intent, which is a telephone number. Thetel:
scheme indicates that the data is a phone number.
Example output:
tel:123
Conclusion:
The am
command is a powerful tool for managing activities and intents in the Android system. With the examples provided, you should now have a better understanding of how to use the am
command to start activities, pass data, match actions and categories, and convert intents to URIs. This command is invaluable for developers who want to automate tasks or perform specific actions within Android apps from the command line.