繁体   English   中英

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

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

很抱歉,这个问题可能很愚蠢,但是我花了将近两天的时间来弄清楚这个问题。

我有这个示例代码,通过调用外壳程序命令“ input tap 807 730”来模拟屏幕触摸。 而且有效。

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

    }

}

我有两种情况。

1)我在onCreate()中调用此代码。 在这种情况下,所有工作都很出色。 为了进行测试,我在整个屏幕上都有一个大按钮,可以看到在命令后如何单击此按钮。

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

接下来发生的事情是:

1)对话框,它有两个按钮,已打开。 我知道数字801和730是按钮“激活”的坐标。 当我单击“设备管理员”对话框的“激活”按钮时,通过在“开发人员选项”中启用“显示指针位置”可以得到这些数字。

2)延迟(2秒)后,我在logcat中看到我的线程确实起作用。

3)按钮“激活”没有任何触摸/点击。 为什么?

所以我的问题是为什么?

但是,如果我将使用ADB这样调用输入命令

./adb shell input tap 807 730

那么“激活”按钮将被触摸。 那么,为什么我的线程命令不起作用。

您可以从应用程序内到达的外壳是沙盒。 出于明显的安全原因,您不能将输入事件发送到系统对话框。 您可以通过以超级用户身份执行命令来绕过root用户的设备。

暂无
暂无

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

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