【问题标题】:Android shell command has strange behavior ("input tap x y")Android shell 命令有奇怪的行为(“input tap x y”)
【发布时间】:2023-03-14 02:57:01
【问题描述】:

抱歉,这个问题可能很愚蠢,但我花了将近两天的时间试图找出问题所在。

我有这个示例代码,我通过调用 shell 命令“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

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

【问题讨论】:

  • 我想 Android 会设置一些限制以防止应用程序授予自己设备管理员权限......所以我认为运行“输入点击”命令的应用程序只会将该点击发送到本身,而不是系统对话框覆盖。然而,adb shell 没有这样的限制。
  • 谢谢。正如我所想的:(认为可以以这种方式使用输入命令真的很愚蠢。但是您可能知道如何强制用户以设备管理员的身份创建应用程序吗?

标签: android shell input touch emulation


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。所以当我从笔记本电脑的终端使用“./adb shell input x y”时,我有更多的权限吗?
  • 是的。 adb 路由通过系统,因此您拥有系统应用程序的权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 2012-04-05
  • 2012-06-21
  • 2022-01-05
  • 2015-12-07
相关资源
最近更新 更多