Android 如何将参数传递给使用 adb shell am Instrumentation 命令启动的测试函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3228245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I deliver parameters to a test function, that launched using adb shell am Instrumentation command
提问by ilana
I am developing in Android, I am using instrumentation to test Phone application. Instrumentation is Android env to test applications.
我正在 Android 中开发,我正在使用仪器来测试电话应用程序。Instrumentation 是用于测试应用程序的 Android 环境。
For that I use am command with name of test case. I run adb, then I enter adb shell, then write in shell the am command.
为此,我使用带有测试用例名称的 am 命令。我运行 adb,然后进入 adb shell,然后在 shell 中写入 am 命令。
I wish to deliver a parameter together with this am command. I mean that I wish to deliver parameters to the test launched by the am command.
我希望与这个 am 命令一起传递一个参数。我的意思是我希望向 am 命令启动的测试传递参数。
Is it possible ??? Please help ?
是否可以 ???请帮忙 ?
回答by Ryan Conrad
you can pass a data uri, mime type and even "extras" to the am command.
您可以将数据 uri、mime 类型甚至“额外”传递给am 命令。
am [start|instrument]
am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>
am [开始|仪器]
am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[ -e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>
You could pass them as "extras" and then get the extras that are passed to it.
您可以将它们作为“额外内容”传递,然后获取传递给它的额外内容。
You would pass them like this:
你会像这样传递它们:
am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT
-e foo bar -e bert ernie -n my.package.component.blah
then in your code:
然后在你的代码中:
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" );
}
回答by Tony Nutter
Pass the paramater in: (e.g., -e peerID SCH-I545)
传入参数:(例如,-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
In the test class:
在测试类中:
{
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");
}
}
回答by Quang Binh Nguyen
exactly is:
正是:
./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 to your main class activity start app. will pass to your app param user_id = 1 and on class SelectCardActivity you get it as bellow :
com.shortcut.activity/com.shortcut.activity.SelectCardActivity-> uir到您的主类活动启动应用程序。将传递给您的应用程序参数 user_id = 1 并且在 SelectCardActivity 类上,您将获得如下结果:
Bundle installparams = this.getIntent ( ).getExtras ( );
回答by user1901015
Since you are already working on Android sdk, given you know the sdk location on your system - Go to sdk location on terminal(command prompt)-> type adb shell -> type am help
由于您已经在使用 Android sdk,假设您知道系统上的 sdk 位置 - 转到终端上的 sdk 位置(命令提示符)-> 输入 adb shell -> 输入 am help
with example http://whenpridefwors.blogspot.in/2011/12/android-send-broadcast-intents-via-adb.html
例如 http://whenpridefwors.blogspot.in/2011/12/android-send-broadcast-intents-via-adb.html