我想将触摸事件注入设备。我为此使用仪器。该方法适用Jelly Bean
于Lollipop
.
当我搜索时,我发现这可能是由于执行SELinux
它会阻止出于安全目的执行某些操作。我下载SELinux Mode Changer
并设置SELinux
为permissive
,并permissive
通过About phone
在settings
. 我的设备已植根,我尝试过使用su
和不使用它。但是,我真的不知道问题出在哪里。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById (R.id.face);
try {
Runtime.getRuntime().exec("supoliciy –live \"allow appdomain input_device dir ( ioctl read getattr search open )\" \"allow appdomain input_device dir ( ioctl read write getattr lock append open )\"");
//supoliciy –live "allow appdomain input_device dir ( ioctl read getattr search open )" "allow appdomain input_device dir ( ioctl read write getattr lock append open )"
// Runtime.getRuntime().exec("reboot");
// Runtime.getRuntime().exec("su -c reboot");
/*
Process process = Runtime.getRuntime().exec("su");//supolicy --live \"allow appdomain input_device dir { ioctl read getattr search open }\" \"allow appdomain input_device chr_file { ioctl read write getattr lock append open }\"");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
String cmd = "/system/bin/input tap 100 200\n";
os.writeBytes(cmd);
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
*/
Process process = Runtime.getRuntime().exec("su");//supolicy --live \"allow appdomain input_device dir { ioctl read getattr search open }\" \"allow appdomain input_device chr_file { ioctl read write getattr lock append open }\"");
}
catch (IOException e) {
Toast toast = Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
// Log.e(" ", e.getStackTrace().toString());
}
/*
used in cmd case only
catch (InterruptedException e){
Toast toast = Toast.makeText(getApplicationContext(), "ERROR 2", Toast.LENGTH_LONG);
toast.show();
}
*/
final Instrumentation m_Instrumentation = new Instrumentation();
final MotionEvent down = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 400, 600, 0);
down.setSource(InputDevice.SOURCE_TOUCHSCREEN);
final MotionEvent up = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 400, 600, 0);
up.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendPointerSync(down);
m_Instrumentation.sendPointerSync(up);
}
});
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast toast = Toast.makeText(getApplicationContext(), "View touched", Toast.LENGTH_LONG);
toast.show();
return false;
}
});
t.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast toast = Toast.makeText(getApplicationContext(), "View touched", Toast.LENGTH_LONG);
toast.show();
return true;
}
}
supoliciy
正如您在上面的代码中看到的那样,我什至尝试执行,但没有任何效果。
我怎么解决这个问题?