【问题标题】:Why simulating touch events in Lollipop doesn't work为什么在 Lollipop 中模拟触摸事件不起作用
【发布时间】:2016-07-03 10:32:09
【问题描述】:

我想将触摸事件注入设备。我为此使用仪器。该方法在Jelly Bean 上运行良好,但它不会注入也不会在Lollipop 中给出任何错误。

当我搜索时,我发现这可能是由于 SELinux 的强制执行导致出于安全目的而阻止执行某些操作。我下载了SELinux Mode Changer 并将SELinux 设置为permissive,并通过在settings 中检查About phone 中的状态来确保将其设置为permissive。我的设备已植根,我尝试过使用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,如您在上面的代码中所见,但没有任何效果。

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

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

    错误应该类似于:

    04-12 19:57:31.501 4885-4885/? E/审计:type=1400 msg=audit(1460509051.501:7365):avc: denied { read } for pid=18086 comm="yourappname" name="lux" 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 命令。在此示例中,您将使用“allow untrusted_app sysfs_sensor_writable file { read }”(在这种情况下不需要大括号,但您可能会在此处添加多个权限)。

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多