27

我正在使用 Android 进行开发,我正在使用仪器来测试电话应用程序。Instrumentation 是用于测试应用程序的 Android 环境。

为此,我使用带有测试用例名称的命令。我运行 adb,然后我进入 adb shell,然后在 shell 中写入 am 命令。

我希望与这个 am 命令一起提供一个参数。我的意思是我希望将参数传递给 am 命令启动的测试。

可能吗 ???请帮忙 ?

4

5 回答 5

51

您可以将数据 uri、mime 类型甚至“附加”传递给am 命令

上午 [开始|仪器]

我开始 [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[ -e <extra_key> <extra_value> ...]
[-n <组件>] [-D] [<uri>]

我的仪器 [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>

您可以将它们作为“附加项”传递,然后获取传递给它的附加项。

你会像这样传递它们:

am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT 
  -e foo bar -e bert ernie -n my.package.component.blah

然后在您的代码中:

Bundle extras = this.getIntent ( ).getExtras ( );

if ( extras != null ) {
  if ( extras.containsKey ( "foo" ) ) {
    Log.d ( "FOO", extras.getString ( "foo" ) );
  } else {
    Log.d ( "FOO", "no foo here" );
  }
  
  if ( extras.containsKey ( "bert" ) ) {
    Log.d ( "BERT", extras.getString ( "bert" ) );
  } else {
    Log.d ( "BERT", "Bert is all alone" );
  }
} else {
  this.setTitle ( "no extras found" );
}
于 2010-07-12T13:41:35.653 回答
15

传入参数:(例如,-e peerID SCH-I545)

adb -s 0915f98870e60701 shell am instrument -w -e class      /
com.example.android.testing.uiautomator.BasicSample.sendInvite /
-e peerID SCH-I545 /
com.example.android.testing.uiautomator.BasicSample.test/android.sup /
port.test.runner.AndroidJUnitRunner

在测试类中:

{
    Bundle extras = InstrumentationRegistry.getArguments();
    String peerID = null;

    if ( extras != null ) {
        if ( extras.containsKey ( "peerID" ) ) {
            peerID = extras.getString("peerID");
            System.out.println("PeerID: " + peerID);
        } else {
            System.out.println("No PeerID in extras");
        }
    } else {
        System.out.println("No extras");
    }
}
于 2016-01-11T16:44:47.683 回答
2

要发送额外价值,您应该添加 -n(Component) 以使用 -e 发送额外价值

这是发送多个键值的示例

adb shell am start -n com.example.jk.checkwifi/.MainActivity -e "imei" $myimei -e "ip" $IP

然后在活动中获取数据,在 onCreate 中获取这样的数据

ip = intent.getStringExtra("ip")
于 2021-02-11T15:08:11.757 回答
0

确切地说是:

 ./adb shell am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e user_id 1 -n com.shortcut.activity/com.shortcut.activity.SelectCardActivity

com.shortcut.activity/com.shortcut.activity.SelectCardActivity -> uir 到您的主类活动启动应用程序。将传递给您的应用程序参数 user_id = 1 并在 SelectCardActivity 类上您得到它,如下所示:

  Bundle installparams = this.getIntent ( ).getExtras ( );
于 2014-01-20T21:31:54.410 回答
0

由于您已经在使用 Android sdk,假设您知道系统上的 sdk 位置 - 转到终端上的 sdk 位置(命令提示符)-> 键入 adb shell -> 键入 am help

http://whenpridefucks.blogspot.in/2011/12/android-send-broadcast-intents-via-adb.html为例

于 2015-03-13T08:28:57.513 回答