Start Android activity from command line with extr

2019-04-23 14:31发布

问题:

This question already has an answer here:

  • How can I deliver parameters to a test function, that launched using adb shell am Instrumentation command 4 answers

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

回答1:

The right command should be

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

with -n....