从命令行启动Android活动 [英] Start Android activity from command line with extra

查看:111
本文介绍了从命令行启动Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!"

然后接收活动中的消息, intent.getExtras()返回null。

and then receive the message in activity, intent.getExtras() returns 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");
}

结果:

SimpleActivity(12345): Intent is not null
SimpleActivity(12345): Bundle is null


推荐答案

正确的命令应该是

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

带有 -n ....

这篇关于从命令行启动Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆