[英]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.