How to start a software project programmatically using Android Intent?

One way to run a test test is

adb shell am instrument -w com.google.vishal.test/android.test.InstrumentationTestRunner 

I want to start with Android code (with intent)

eg,

 adb shell am start -n com.google.android.contacts/.ContactsActivity 

we can run using Android intent as follows: -

 Intent intent = new Intent(com.google.android.contacts, ContactsActivity.class); startActivity(intent); 

But how to run

 adb shell am instrument -w com.google.vishal.test/android.test.InstrumentationTestRunner 

according to the intentions of Android?

Thanks for your help in advance :-)

+4
source share
2 answers

The command to run the toolkit from the adb shell : -

 adb shell am instrument -w com.android.vishal.project.test/android.test.InstrumentationTestRunner 

Android code to run the toolkit from Android Activity : -

  startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null); 

Note:

Another method

Android code to run the toolkit (Android 2.3 to Android 4.1.2)

 String str_cmd = "adb shell am instrument -w com.android.vishal.project.test/android.test.InstrumentationTestRunner"; Runtime.getRuntime().exec(str_cmd); 

Android 4.2 requires the permission "android.permission.INJECT_EVENTS" and is only allowed by the system application. The user application cannot use this permission due to some security reasons.

therefore you cannot use Runtime.getRuntime (). exec (str_cmd); for Android 4.2 onwards ...

so now the working method:

  startInstrumentation(new ComponentName("com.android.vishal.project.test", "android.test.InstrumentationTestRunner"), null, null); 

run this command from your activity.

Thanks.

+15
source

In fact, this is impossible to do, the reason is that adb has certain special privileges because of security and it cannot be launched on the phone (as with any open source code) to run the tools you need using ADB , of course, it is possible, but you will have to rewrite some android, and then it will only work on the phones that you installed on this!).

May I ask your reason for this? If you really need to automate all applications, the best choice may be either the new Android testing shell, or testing only on an emulator and using something that runs on top of the view hierarchy, because trying to make you currently a dead end.

+2
source

All Articles