gpt4 book ai didi

android - ADB与Android App通信方式

转载 作者:行者123 更新时间:2023-11-29 21:33:25 24 4
gpt4 key购买 nike

我正在开发一个可以从 adb 控制台进行定时控制的应用程序。 'adb shell sendevent' 命令控制应用程序的触摸事件。但是这个触摸事件是定时的,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")

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

最佳答案

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

您需要在您的 list 中注册您的BroadcastReceiver,如下所示:

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

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

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

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

注意:如果应用程序被最小化,并且您正在尝试模拟触摸事件,应用程序将强制关闭,因为 android 开发人员限制了这个 future ,因为它很容易被滥用黑客。

关于android - ADB与Android App通信方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18911401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com