简体   繁体   English

Android Shell命令具有异常行为(“ input tap xy”)

[英]Android shell command has strange behavior (“input tap x y”)

Sorry for maybe silly question, but I spent almost two days trying figure out the problem. 很抱歉,这个问题可能很愚蠢,但是我花了将近两天的时间来弄清楚这个问题。

I have this sample code where I emulate screen touch by invoking shell command "input tap 807 730". 我有这个示例代码,通过调用外壳程序命令“ input tap 807 730”来模拟屏幕触摸。 And it works. 而且有效。

public class DisableRequest extends Thread {
    @Override
    public void run() {
        super.run();
        StringBuffer output = new StringBuffer();

        try {
            Thread.sleep(3000);

            Process p = Runtime.getRuntime().exec("input tap 807 730");
            p.waitFor();

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";

            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
        } catch (Exception e) {
            Log.v("INPUT_TEST", "Error : " + e.toString());
        }

        String response = output.toString();
        Log.v("INPUT_TEST", "Response : " + response);

    }

}

I have two cases. 我有两种情况。

1 ) I invoke this code in onCreate(). 1)我在onCreate()中调用此代码。 In this case all work excellent. 在这种情况下,所有工作都很出色。 For a test I have a big button all over the screen and I can see how this button is clicked after command. 为了进行测试,我在整个屏幕上都有一个大按钮,可以看到在命令后如何单击此按钮。

2 ) I invoke this code also in onCreate() but after that I invoke this code to open dialog to request Device Administrator. 2)我也在onCreate()中调用此代码,但此后,我调用此代码打开对话框以请求设备管理员。

private void askForAdministrator() {
    //Begin enabling device administrator
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    ComponentName deviceAdminComponentName = new ComponentName(this, MyAdminReceiver.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You must enable device administration for certain features"
            + " of the app to function.");

    //I thought that this would start the activity that lets the user
    //choose whether to enable the app as a device admin
    startActivityForResult(intent, ACTIVATION_REQUEST);
}

Here's what happened next: 接下来发生的事情是:

1) Dialog, it has two buttons, opened. 1)对话框,它有两个按钮,已打开。 I know that the numbers 801 and 730 are coordinates of button "Activate". 我知道数字801和730是按钮“激活”的坐标。 I got these numbers from enabling "Show pointer location" in "Developer options" when I clicked on "Activate" button of that Device Administrator dialog. 当我单击“设备管理员”对话框的“激活”按钮时,通过在“开发人员选项”中启用“显示指针位置”可以得到这些数字。

2) After delay (2 sec) I can see in logcat that my thread did work. 2)延迟(2秒)后,我在logcat中看到我的线程确实起作用。

3) The button "Activate" didn't get any touches/clicks. 3)按钮“激活”没有任何触摸/点击。 WHY? 为什么?

So my question is WHY? 所以我的问题是为什么?

BUT, if I will invoke input command using ADB like this 但是,如果我将使用ADB这样调用输入命令

./adb shell input tap 807 730

then the "Activate" button will get touche. 那么“激活”按钮将被触摸。 So why my command from thread didn't work. 那么,为什么我的线程命令不起作用。

The shell you can reach from within an app is sandboxed. 您可以从应用程序内到达的外壳是沙盒。 You can not send input events to system dialogs for obvious security reasons. 出于明显的安全原因,您不能将输入事件发送到系统对话框。 You can bypass this on a rooted device by executing the command as superuser. 您可以通过以超级用户身份执行命令来绕过root用户的设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM