为什么在Lollipop中模拟触摸事件不起作用

时间:2016-03-17 07:48:24

标签: android selinux android-instrumentation android-kernel android-touch-event

我想将触摸事件注入设备。我使用仪器。该方法适用于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,正如您在上面的代码中看到的那样,但没有任何效果。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你看到了什么错误? logcat 应该告诉你它被拒绝的原因。您可能缺少一些其他权限。此外,应使用花括号而不是括号将权限组合在一起。

错误应该类似于:

  

04-12 19:57:31.501 4885-4885 /? E / audit:type = 1400 msg = audit(1460509051.501:7365):avc:拒绝{read} for pid = 18086 comm =" yourappname"名称="勒克斯" dev的=" sysfs的" ino = 19601 scontext = u:r:untrusted_app:s0:c512,c768 tcontext = u:object_r:sysfs_sensor_writable:s0 tclass = file permissive = 0

然后,您可以使用它来修复/更新您的supolicy命令。在此示例中,您将使用" 允许untrusted_app sysfs_sensor_writable文件{read} " (在这种情况下不需要大括号,但你可能会在这里添加多个权限。)