Skip to content Skip to sidebar Skip to footer

Android Programmatically Trigger Long Home Press

I am trying to figure out a way to programmatically mimic the action of a long HOME button press. I have BACK button working with following code: this.dispatchKeyEvent(new KeyEvent

Solution 1:

You can try this:

this.dispatchKeyEvent(newKeyEvent((long) ViewConfiguration.getLongPressTimeout(), (long) 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME), 0); // ViewConfiguration.getLongPressTimeout() returns the duration in milliseconds before a press turns into a long press

It uses this constructor:

/**
 * Create a new key event.
 *
 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
 * at which this key code originally went down.
 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
 * at which this event happened.
 * @param action Action code: either {@link #ACTION_DOWN},
 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
 * @param code The key code.
 * @param repeat A repeat count for down events (> 0 if this is after the
 * initial down) or event count for multiple events.
 */public KeyEvent(long downTime, long eventTime, int action,
                int code, int repeat) {
    mDownTime = downTime;
    mEventTime = eventTime;
    mAction = action;
    mKeyCode = code;
    mRepeatCount = repeat;
    mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
}

See this. It might help.

Post a Comment for "Android Programmatically Trigger Long Home Press"