android长按系统按键

/ 测试开发 / 0 条评论 / 811浏览
原理:
http://blog.csdn.net/u012897401/article/details/53319022
http://blog.csdn.net/sinat_29052561/article/details/53643433
通过uiautomatorbrider来调用inject事件

1.
public static boolean longPressKeyCode(int keyCode,int PressTime) {
          try {
               Field mUiAutomationBridge = Class.forName("com.android.uiautomator.core.UiDevice").getDeclaredField("mUiAutomationBridge"); 
               mUiAutomationBridge.setAccessible(true); 
    
               Object bridgeObj = mUiAutomationBridge.get(UiDevice.getInstance());
               Method injectInputEvent = Class.forName("com.android.uiautomator.core.UiAutomatorBridge").getDeclaredMethod("injectInputEvent",new Class[]{android.view.InputEvent.class,boolean.class}); 
      
               final long eventTime = SystemClock.uptimeMillis(); 
               KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,  keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
               if ((Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{downEvent, true})) { 
                    SystemClock.sleep(PressTime*1000);
                    KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
                    if ((Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{upEvent, true})) { 
                         return true; 
                    }
               }
          } catch (NoSuchMethodException e) { 
               e.printStackTrace(); 
          } catch (InvocationTargetException e) { 
               e.printStackTrace(); 
          } catch (NoSuchFieldException e) { 
               e.printStackTrace(); 
          } catch (IllegalAccessException e) { 
               e.printStackTrace(); 
          } catch (ClassNotFoundException e) { 
               e.printStackTrace(); 
          } 
          return false; 
     } 
2. 
//长按菜单键
boolean longpressRes = MdmHelper.longPressKeyCode(82, 4); 
、、、、、、、、、、、、、、、、、、、、、
public static boolean pastText() {
        try {
             Field mUiAutomationBridge = Class.forName("com.android.uiautomator.core.UiDevice").getDeclaredField("mUiAutomationBridge");
             mUiAutomationBridge.setAccessible(true);
 
             Object bridgeObj = mUiAutomationBridge.get(UiDevice.getInstance());
             Method injectInputEvent = Class.forName("com.android.uiautomator.core.UiAutomatorBridge").getDeclaredMethod("injectInputEvent",new Class[]{android.view.InputEvent.class,boolean.class});
   
             //final long eventTime = SystemClock.uptimeMillis();
             KeyEvent downEvent_M = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,  KeyEvent.KEYCODE_MENU, 0);
             KeyEvent downEvent_V = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,  KeyEvent.KEYCODE_V, 0);
             if ((Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{downEvent_M, true}) && (Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{downEvent_V, true})) {
                  KeyEvent upEvent_M = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0);
                  KeyEvent upEvent_V = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_V, 0);
                  if ((Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{upEvent_M, true}) && (Boolean) injectInputEvent.invoke(bridgeObj, new Object[]{upEvent_V, true})) {
                          try {
                                   Thread.sleep(300);
                              } catch (InterruptedException e) {
                                   e.printStackTrace();
                              }    
                              KeyEvent downEvent_M2 = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,  KeyEvent.KEYCODE_MENU, 0);
                              injectInputEvent.invoke(bridgeObj, new Object[]{downEvent_M2, true});
                              KeyEvent upEvent_M2 = new KeyEvent(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0);
                              injectInputEvent.invoke(bridgeObj, new Object[]{upEvent_M2, true});
                              return true;
                  }
             }
        } catch (NoSuchMethodException e) {
             e.printStackTrace();
        } catch (InvocationTargetException e) {
             e.printStackTrace();
        } catch (NoSuchFieldException e) {
             e.printStackTrace();
        } catch (IllegalAccessException e) {
             e.printStackTrace();
        } catch (ClassNotFoundException e) {
             e.printStackTrace();
        }
        return false;
   }