Siddharthan Asokan
Siddharthan Asokan

Reputation: 4441

ADB and Android App communication mode

I'm working on an app which has timed control from adb console. The 'adb shell sendevent' command controls the touch event of the app. But this touch event is timed and the commnd

import os


apkLocation = "/Users/siddharthan64/Desktop/adt/sdk/platform-tools/"
os.chdir(apkLocation)

cmdList = ['./adb shell sendevent /dev/input/event2: 3 53 251','./adb shell sendevent /       dev/input/event2: 3 54 399','./adb shell sendevent /dev/input/event2: 3 48 10','./adb shell  sendevent /dev/input/event2: 3 50 7','./adb shell sendevent /dev/input/event2: 0 2 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 50 7']
cmdList.append[,'./adb shell sendevent /dev/input/event2: 0 2 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 0 0 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 57 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 53 251']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 54 399']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 48 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 3 50 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 0 2 0']
cmdList.append['./adb shell sendevent /dev/input/event2: 0 0 0']
for tcmd in cmdList:
p = os.popen(tcmd,"r")

is sent when a certain event happens on the app.

Upvotes: 1

Views: 1720

Answers (1)

Zax
Zax

Reputation: 3010

Actually the sendevent differs from device to device. This is from my personal experience. If you are trying to simulate touch events, then you can use the concept of Instrumentation in Android.

So, you need to have a BroadcastReceiver that receives input touch co-ordinates from the adb shell.

Below is the code for BroadcastReceiver which has the instrumentation code:

public void onReceive(Context arg0, Intent i) {
        // TODO Auto-generated method stub
        //Toast.makeText(arg0, "Broadcast intent received...", Toast.LENGTH_SHORT).show();

        String args=i.getStringExtra("vals");
        String[] arr=args.split(" ");
        final int myVal1=Integer.parseInt(arr[0]);
        final int myVal2=Integer.parseInt(arr[1]);
        //Toast.makeText(arg0, "vals:"+args, Toast.LENGTH_SHORT).show();
        //Toast.makeText(arg0, "myVal1="+myVal1+"\nmyVal2="+myVal2, Toast.LENGTH_SHORT).show();

        Thread myThread=new Thread()
        {
            public void run() {
                Instrumentation myInst = new Instrumentation();
                myInst.sendKeyDownUpSync( KeyEvent.KEYCODE_B );

                myInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,myVal1, myVal2, 0));

                myInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,myVal1, myVal2, 0));
            };
        };
        myThread.start();
    }

You need to register your BroadcastReceiver in you manifest as shown below:

<receiver android:name="MyReceiver" android:exported="true">
    <intent-filter>
      <action android:name="android.intent.action.VIEW" /> 
    </intent-filter>
</receiver>

Once your app is up, connect your device in debug mode, in the command prompt, put the following command:

adb shell am broadcast --es vals "10 20" -n com.pkgName.appName/com.pkgName.appName.MyReceiver

The above command will trigger the BroadcstReceiver of your application, and a touch will be simulated at co-ordinates (10,20). You can replace it with the values you want.

Note: If the app is minimized, and you are trying to simulate the touch events, the app will force close, as the android developers have restricted this future, as it can be easily misused by hackers.

Upvotes: 2

Related Questions