1

我正在开发一个从 adb 控制台进行定时控制的应用程序。'adb shell sendevent' 命令控制应用程序的触摸事件。但是这个触摸事件是定时的,并且命令

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")

当应用程序上发生特定事件时发送。

4

1 回答 1

2

实际上sendevent,设备与设备不同。这是我个人的经验。如果您尝试模拟触摸事件,那么您可以使用InstrumentationAndroid 中的概念。

所以,你需要有一个从外壳BroadcastReceiver接收输入触摸坐标的。adb

以下是BroadcastReceiver具有检测代码的代码:

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();
    }

您需要BroadcastReceiver在您的帐户中注册manifest,如下所示:

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

应用启动后,以调试模式连接设备,在命令提示符下输入以下命令:

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

上面的命令将触发BroadcstReceiver您的应用程序,并在坐标 (10,20) 处模拟触摸。您可以将其替换为您想要的值。

注意:如果应用程序被最小化,并且你试图模拟触摸事件,应用程序将强制关闭,因为安卓开发者已经限制了这个未来,因为它很容易被黑客滥用。

于 2013-09-20T08:20:54.583 回答