下面列出了android.app.Instrumentation#sendKeyDownUpSync ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void startFakeTouchTask() {
// Solution/Hack #3 - "Send fake touches!"
// https://www.youtube.com/watch?v=F2ZDp-eNrh4
mTimer = new Timer();
mSendFakeTouchTask = new TimerTask() {
@Override
public void run() {
try {
Instrumentation instrumentation = new Instrumentation();
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACKSLASH);
} catch (SecurityException e) {
// ignore SecurityException
// "Injecting to another application requires INJECT_EVENTS permission"
}
}
};
mTimer.schedule(mSendFakeTouchTask, 1000, 1000);
}
/**
* Taps the negative button of a currently displayed dialog. This method assumes that a button of
* the dialog is currently selected.
*
* @see #tapDialogPositiveButton(Instrumentation)
*/
public static void tapDialogNegativeButton(Instrumentation instrumentation) {
selectDialogButton(instrumentation);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
/**
* Taps the positive button of a currently displayed dialog. This method assumes that a button of
* the dialog is currently selected.
*
* @see #tapDialogNegativeButton(Instrumentation)
*/
@FixWhenMinSdkVersion(14)
public static void tapDialogPositiveButton(Instrumentation instrumentation) {
selectDialogButton(instrumentation);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
/**
* Selects a button at the bottom of a dialog. This assumes that a dialog is currently displayed
* in the foreground.
*/
private static void selectDialogButton(Instrumentation instrumentation) {
// If the dialog contains too much text it will scroll and the buttons at the bottom will only
// get selected once it scrolls to the very bottom.
// So far, 6 x DPAD_DOWN seems to do the trick for our app...
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}
/**
* Taps the negative button of a currently displayed 3 button dialog. This method assumes that a
* button of the dialog is currently selected.
*/
public static void tapNegativeButtonIn3ButtonDialog(Instrumentation instrumentation) {
selectDialogButton(instrumentation);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
/**
* Taps the neutral button of a currently displayed 3 button dialog. This method assumes that a
* button of the dialog is currently selected.
*/
public static void tapNeutralButtonIn3ButtonDialog(Instrumentation instrumentation) {
selectDialogButton(instrumentation);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
/**
* Taps the positive button of a currently displayed 3 button dialog. This method assumes that a
* button of the dialog is currently selected.
*/
public static void tapPositiveButtonIn3ButtonDialog(Instrumentation instrumentation) {
selectDialogButton(instrumentation);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
@Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
}
/**
* Force the device to think that a hardware button has been pressed, this is used for Play/Pause/Previous/Next Media commands
* @param keyCode keycode for the hardware button we're emulating
*/
private static void sendMediaButton(int keyCode) {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
}
/**
* Force the device to think that a hardware button has been pressed, this is used for Play/Pause/Previous/Next Media commands
* @param context
* @param keyCode keycode for the hardware button we're emulating
*/
private static void sendMediaButton(Context context, int keyCode) {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
}