How is it possible to run monkey from android app

1.1k views Asked by At

I try to run 'adb shell monkey' from my app for another app using the code below:

String[] command = null;
        command = new String[] {
            "monkey", "-p", "com.example.appname", "-v", "500" };

        if(command != null){

            Process process = Runtime.getRuntime().exec(command);

            process.waitFor();
            dumpStream(process.getInputStream());
            dumpStream(process.getErrorStream());

            LOGGER.error("" + process.exitValue());


            if(process.exitValue() == 0 ){
            }
        }

But it returns me only:

12-11 14:45:47.525: E/MainActivity: main(15436): App /data/data/com.example.app/files/adb -> true

12-11 14:45:47.565: E/MainActivity: main(15436): App adb exit value: 0

12-11 14:45:51.149: E/MainActivity: main(15436): App /data/data/com.example.app/files/adb -> true

12-11 14:46:03.172: E/MainActivity: main(15436): App:Monkey: seed=1387859337757 count=500

12-11 14:46:03.172: E/MainActivity: main(15436): App:AllowPackage: com.example.appname

12-11 14:46:03.172: E/MainActivity: main(15436): App:IncludeCategory: android.intent.category.LAUNCHER

12-11 14:46:03.182: E/MainActivity: main(15436): App:IncludeCategory: android.intent.category.MONKEY

With exit code : 9

Maybe anybody knows how is it possible to run 'monkey' from android application?

1

There are 1 answers

0
insomniac On

To run monkey from an android app you need to add the permission

<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER"/>

to the Manifest File,

Which allows an application to watch and control how activities are started globally in the system. Only for is in debugging (usually the monkey command).

And unfortunately this permission is only available for the system applications.that is, it can only be applicable to applications which are

  1. system - preinstalled in the device's firmware

or
2. apps which are signed with the "platform key", i.e. the same key that was used to sign the firmware.