Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Android activity from command line with extra [duplicate]

I created a simple activity that I want to start from command line and pass in some value from command line.

However, if I try to do

adb shell am start com.example.mike.app/.SimpleActivity --es "Message" "hello!"

and then receive the message in activity, intent.getExtras() returns null.

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    Log.d(LOGTAG, intent == null ? "Intent is null" : "Intent is not null");
    Log.d(LOGTAG, bundle == null ? "Bundle is null" : "Bundle is not null");
}

Result:

SimpleActivity(12345): Intent is not null
SimpleActivity(12345): Bundle is null
like image 901
Mike Lee Avatar asked Nov 14 '15 01:11

Mike Lee


People also ask

How do I launch an activity from adb?

You can use the start command from Activity Manager (am) a.k.a the adb shell am start -n command (via adb) specifying the app package name and the component name as defined in the manifest. You can add other parameters like ACTION (-a android. intent. action.

How do I use the AM command?

am command : start. Like --start-profiler, but profiling stops when the app goes idle. Repeat the activity launch <COUNT> times. Prior to each repeat, the top activity will be finished.

What is Intent FLAG_ activity_ new_ task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .

What is task affinity Android?

▶ Task affinity specifies which task that the activity desires. to join. By default, all activities in an app have the same. affinity – the app package name. <manifest xmlns:android="http://schemas.android.com/apk/res/android"


1 Answers

The right command should be

adb shell am start -n com.example.mike.app/.SimpleActivity --es "Message" "hello!"

with -n....

like image 84
Mike Lee Avatar answered Oct 01 '22 07:10

Mike Lee