Android: 发送虚拟按键

长平狐 发布于 2012/06/20 11:03
阅读 1K+
收藏 0
    /**
     * Send a single key event.
     *
     * @param event is a string representing the keycode of the key event you
     * want to execute.
     */
    private void sendKeyEvent(int keyCode) {
        int eventCode = keyCode;
        long now = SystemClock.uptimeMillis();
        try {
            KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, eventCode, 0);
            KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, eventCode, 0);
            (IWindowManager.Stub
                .asInterface(ServiceManager.getService("window")))
                .injectInputEventNoWait(down);
            (IWindowManager.Stub
                .asInterface(ServiceManager.getService("window")))
                .injectInputEventNoWait(up);
        } catch (RemoteException e) {
            Log.i(TAG, "DeadOjbectException");
        }
    }
sendKeyEvent(KeyEvent.KEYCODE_BACK);

原文链接:http://blog.csdn.net/zmyde2010/article/details/7096287
加载中
返回顶部
顶部