简体   繁体   中英

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". 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(). 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.

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. I know that the numbers 801 and 730 are coordinates of button "Activate". 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.

3) The button "Activate" didn't get any touches/clicks. WHY?

So my question is WHY?

BUT, if I will invoke input command using ADB like this

./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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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