android 應用模擬按鍵

 

    private void simulateKeystroke(int KeyCode) {

         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));

         doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));

    }

 

 

private void doInjectKeyEvent(KeyEvent kEvent) {

try {

/* Inject the KeyEvent to the Window-Manager. */

/* The WindowManager capable of injecting keyStrokes. */

final IWindowManager windowManager = IWindowManager.Stub

.asInterface(ServiceManager.getService("window"));

 

windowManager.injectKeyEvent(kEvent.isDown(), kEvent.getKeyCode(),

kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent

.getEventTime(), true);

} catch (DeadObjectException e) {

e.printStackTrace();

}

}

發佈了23 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章