天天看點

Android getevent攔截輸入事件

上篇文章《Android AccessibilityService攔截事件及VR眼鏡傳回按鍵捕捉》我們介紹了如何用AccessibilityService來攔截事件。

本篇我們來介紹另一種攔截按鍵的方式,就是在shell的狀态下,使用getevent指令對輸入事件進行分析。必須要說明的是getevent指令使用場景有:

1.在PC端shell環境下,不必root

2.在APP端使用必須要root權限

package com.jason.btnmonitor;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMonitor();

        Button get = (Button) findViewById(R.id.get);
        Button start = (Button) findViewById(R.id.start);
        Button stop = (Button) findViewById(R.id.stop);


        get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDevices();
            }
        });

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startMonitor();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopMonitor();
            }
        });
    }

    private void getDevices(){
        BtnMonitorController.getInstance().getVRDevice();
    }

    private void initMonitor(){
        BtnMonitorController.getInstance().init();
    }


    public void startMonitor(){

        BtnMonitorController.getInstance().start();

    }


    public void stopMonitor(){

        BtnMonitorController.getInstance().stop();
    }


    private void releaseMonitor(){
        BtnMonitorController.getInstance().release();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMonitor();
    }
}
           

MainActivity的實作。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jason.btnmonitor.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get"
        android:id="@+id/get"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"
        android:id="@+id/start"
        android:layout_toRightOf="@+id/get"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop"
        android:layout_toRightOf="@+id/start"
        android:id="@+id/stop"
        />
</RelativeLayout>
           

layout的實作。

package com.jason.btnmonitor;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.Command;
import com.stericson.RootTools.execution.Shell;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class BtnMonitorController {

    private static final String COMMAND_GET_DEVICE_VR = "getevent";
    private static final String COMMAND_GET_VR_EVENT = "getevent -l -t ";
    public static final int MSG_EVENT_INPUT = 9000;
    private String deviceName="/dev/input/event19"; //demo監聽的是event19,這裡可以改為你所要監聽的event

    private static boolean isMonitorStart = false;
    private static boolean hasVR = false;

    private Shell shell;
    private Command command;
    private Handler handler;

    private static BtnMonitorController instance ;
    
    private BtnMonitorController(){
        
    }
    
    public static BtnMonitorController getInstance(){
        if (instance == null) {
            synchronized (BtnMonitorController.class){
                if (instance == null) {
                    instance = new BtnMonitorController() ;
                }
            }
        }
        return instance ;
    }


    public void init(){
        try {
            shell = RootTools.getShell(true);//getevent指令必須要root
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.e("shell init error");
        } catch (TimeoutException e) {
            e.printStackTrace();
            LogUtil.e("shell init error");

        } catch (RootDeniedException e) {
            e.printStackTrace();
            LogUtil.e("shell init error");
        }

        handler = new Handler() {

            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_EVENT_INPUT:

                        LogUtil.i(""+msg.obj);

                        break;

                }
                super.handleMessage(msg);
            }
        };
    }
//getVRDevice會輸出你的inputdevice的清單,選中你想要監聽的裝置,替換deviceName
    public void getVRDevice(){
        LogUtil.i("BtnMonitor getVRDevice" );
        if(isMonitorStart){
            LogUtil.e("BtnMonitor is started already!");
            return;
        }
        startCMD(COMMAND_GET_DEVICE_VR);

    }

    public void start(){

        LogUtil.i("BtnMonitor start" );
        if(isMonitorStart){
            LogUtil.e("BtnMonitor is started already!");
            return;
        }

        startCMD(COMMAND_GET_VR_EVENT+deviceName);

    }


    public void stop(){
        LogUtil.i("BtnMonitor stop" );
        if(command.isFinished()){
            LogUtil.e("BtnMonitor is stopped already!");
            return;
        }

        command.terminate("stop");
    }


    public void release(){
        LogUtil.i("BtnMonitor release" );
        if(handler != null && handler.hasMessages(MSG_EVENT_INPUT)){
            handler.removeMessages(MSG_EVENT_INPUT);
            handler = null;
        }

        if(isMonitorStart){
            command.terminate("exit");
            isMonitorStart = false;
        }

        try {
            if(shell != null)
            shell.close();
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.e("shell close IOException");
        }

    }

    private void startCMD(String cmd){

        isMonitorStart = true;

        command = new Command(0, 0, cmd) {
            @Override
            public void output(int id, String line) {
                LogUtil.e("MainActivity.java:output, id:" + id + ", output:" + line);

                Message m = new Message();
                m.what = MSG_EVENT_INPUT;
                m.obj = line + "\n";
                handler.sendMessage(m);
            }

            @Override
            public void commandCompleted(int arg0, int arg1) {
                // TODO Auto-generated method stub
                LogUtil.e("MainActivity.java:commandCompleted, arg0:" + arg0 + ", arg1:" + arg1);
                Message m = new Message();
                m.what = MSG_EVENT_INPUT;
                m.obj = "commandCompleted\n" + "================================\n";
                handler.sendMessage(m);

                isMonitorStart = false;
            }

            @Override
            public void commandOutput(int arg0, String arg1) {
                // TODO Auto-generated method stub
                LogUtil.e( "MainActivity.java:commandOutput, arg0:" + arg0 + ", arg1:" + arg1);
                Message m = new Message();
                m.what = MSG_EVENT_INPUT;
                m.obj = "commandOutput, arg0:" + arg0 + ", arg1:" + arg1 + "\n";
                handler.sendMessage(m);
            }

            @Override
            public void commandTerminated(int arg0, String arg1) {
                // TODO Auto-generated method stub
                LogUtil.e("MainActivity.java:commandTerminated, arg0:" + arg0 + ", arg1:" + arg1);
                Message m = new Message();
                m.what = MSG_EVENT_INPUT;
                m.obj = "commandTerminated, reason:" + arg1 + "\n" + "================================\n";
                handler.sendMessage(m);

                isMonitorStart = false;

            }
        };

        try {
            shell.add(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
           

BtnMonitorController的實作,使用的是開源項目的RootTools。github工程位址

https://github.com/graygray/getevent.git

Android getevent攔截輸入事件
$ getevent /dev/input/event19                             
0004 0004 00090002
0001 0111 00000001
0000 0000 00000000
0004 0004 00090002
0001 0111 00000000
0000 0000 00000000
           

監聽到的VR裝置(預設類型為滑鼠)單擊傳回鍵的内容

$ getevent -l -t /dev/input/event19                       
[   17437.061273] EV_MSC       MSC_SCAN             00090002            
[   17437.061273] EV_KEY       BTN_RIGHT            DOWN                
[   17437.061273] EV_SYN       SYN_REPORT           00000000            
[   17437.156346] EV_MSC       MSC_SCAN             00090002            
[   17437.156346] EV_KEY       BTN_RIGHT            UP                  
[   17437.156346] EV_SYN       SYN_REPORT           00000000            
[   17444.748923] EV_MSC       MSC_SCAN             00090002            
[   17444.748923] EV_KEY       BTN_RIGHT            DOWN                
[   17444.748923] EV_SYN       SYN_REPORT           00000000            
[   17446.244327] EV_MSC       MSC_SCAN             00090002            
[   17446.244327] EV_KEY       BTN_RIGHT            UP                  
[   17446.244327] EV_SYN       SYN_REPORT           00000000  
           

監聽到的VR裝置(預設類型為滑鼠)傳回鍵長按的内容,-t加上了時間戳用于判斷是否是長按

針對收到的message内容可以判斷BACK鍵的單擊和長按。