从额外的命令行启动Android活动

Yol*_*and 9 android adb android-intent

我创建了一个简单的活动,我想从命令行启动并从命令行传递一些值.

但是,如果我尝试做的话

adb shell am start com.example.mike.app/.SimpleActivity --es "Message" "hello!"
Run Code Online (Sandbox Code Playgroud)

然后在activity中接收消息,intent.getExtras()返回null.

活动:

@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");
}
Run Code Online (Sandbox Code Playgroud)

结果:

SimpleActivity(12345): Intent is not null
SimpleActivity(12345): Bundle is null
Run Code Online (Sandbox Code Playgroud)

Yol*_*and 14

正确的命令应该是

adb shell am start -n com.example.mike.app/.SimpleActivity --es "Message" "hello!"
Run Code Online (Sandbox Code Playgroud)

-n....

  • @UbaierBhat [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...] (2认同)
  • 有[am start的文档](https://developer.android.com/studio/command-line/adb#IntentSpec)。或者,“ adb shell am help start”查看完整的args列表。 (2认同)