我正在开发一款可以从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")
在应用程序上发生特定事件时发送。
答案 0 :(得分:2)
实际上sendevent
因设备而异。这是我个人的经验。如果您尝试模拟触摸事件,则可以在Android中使用Instrumentation
的概念。
所以,你需要一个BroadcastReceiver
来接收来自adb
shell的输入触摸坐标。
以下是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)处模拟触摸。您可以将其替换为您想要的值。
注意:如果应用程序已最小化,并且您正在尝试模拟触摸事件,则应用程序将强制关闭,因为Android开发人员限制了此未来,因为它很容易被滥用黑客的攻击。</ p>