模拟长按Home键


Spec需要模拟长按Home键。


对KeyEvent.KEYCODE_HOME 跟踪到PhoneWIndowManaer.java中,其中interceptKeyBeforeDispatching()有:

                 if (repeatCount == 0) { 
  
                    mHomePressed = true;
                } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
                    if (!keyguardOn) {
                        handleLongPressOnHome();
                    }
                }

因此,我们需要创建一个KeyEvent ,flags为 KeyEvent.FLAG_LONG_PRESS ,而且repeatCount不为0.


代码:

        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME); 
        keyEvent = keyEvent.changeTimeRepeat(keyEvent,SystemClock.currentThreadTimeMillis(),1,KeyEvent.FLAG_LONG_PRESS);

 try {
           final boolean result =  (IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
                .injectKeyEvent(keyEvent, true);
        } catch (RemoteException e) {
        }


另外需要权限的修改的修改:

Android.mk中添加: LOCAL_CERTIFICATE := platform

AndroidMainfest.xml添加权限:

       

你可能感兴趣的:(android)