ADB和Android应用程序通信模块

2024-09-27 22:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,它有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")

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


Tags: dev命令应用程序inputos事件shelladb
1条回答
网友
1楼 · 发布于 2024-09-27 22:23:00

实际上,sendevent因设备而异。这是我个人的经验。如果您试图模拟触摸事件,那么您可以在Android中使用Instrumentation的概念。在

因此,您需要一个BroadcastReceiveradb外壳接收输入触摸坐标。在

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

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

^{pr2}$

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

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

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

注意:如果应用程序被最小化,并且您试图模拟触摸事件,则该应用程序将强制关闭,因为android开发者限制了这一未来,因为它很容易被黑客滥用。在

相关问题 更多 >

    热门问题