- down -- suspend
- up -- wakeup
原來的 "兩用" power button,用這樣的方法 detect longpress -
private void interceptPowerKeyDown(boolean handled) {
Log.v(TAG,"InterceptPowerKeyDown");
mPowerKeyHandled = handled;
if (!handled) {
Log.v(TAG,"...postDelay...");
mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
在 keydown 時,設定一個 5 sec 啟動的 call back function ...
然後在 key up 時移除他:
private boolean interceptPowerKeyUp(boolean canceled) {
Log.v(TAG,"interceptPowerKeyUp");
if (!mPowerKeyHandled) {
Log.v(TAG,"...remove...");
mHandler.removeCallbacks(mPowerLongPress);
return !canceled;
} else {
mPowerKeyHandled = true;
return false;
}
}
所以如果在 5sec 內,call back function 還沒被 launch 前 relase key 的話, call back function 就會被 remove.. 也就不會執行了。
改為 level 動作 .. 一樣,利用 這種 delay-remove 的方法:
private final Runnable mGoToSleep = new Runnable() {
public void run() {
Log.v(TAG,"mGoToSleep");
mPowerManager.goToSleep(SystemClock.uptimeMillis() + 1);
}
};
然後按下 powerdown 的時候..
private void interceptPowerKeyDown(boolean handled) {
Log.v(TAG,"InterceptPowerKeyDown");
mPowerKeyHandled = handled;
if (!handled) {
Log.v(TAG,"...postDelay...");
mHandler.postDelayed(mGoToSleep,1000);
}
}
private boolean interceptPowerKeyUp(boolean canceled) {
Log.v(TAG,"interceptPowerKeyUp");
if (!mPowerKeyHandled) {
Log.v(TAG,"...remove...");
mHandler.removeCallbacks(mGoToSleep);
return !canceled;
} else {
mPowerKeyHandled = true;
return false;
}
}
另外,interceptKeyBeforeQueueing ( ) 也要改一下..
原來,只有在 'down' 時,才會加入 wakeup ..改成不用了
if (isWakeKey /* && down */) {
if (keyguardActive) {
// If the keyguard is showing, let it decide what to do with the wake key.
mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(keyCode);
Log.d(TAG,"Keyguard");
} else {
// Otherwise, wake the device ourselves.
result |= ACTION_POKE_USER_ACTIVITY;
result |= ACTION_WAKE_TO_SLEEP;
Log.d(TAG,"ADD WAKE");
}
}
這樣在 keyup 時,也會有 WAKE_TO_SLEEP 動作。
result 沒有 WAKE_TO_SLEEP flag 的話, system resume 後又會馬上suspend..其實還有一點是 系統的 pwrbutton level 改成:
- low -- suspend
- high -- resume
- low -- key up
- high -- key down
2 則留言:
請問這個 ACTION_WAKE_TO_SLEEP,在2.3的code裡面好像找不到?
?
有呀,我是用 2.3.4
在
framework/base/core/java/android/view/WindowManagerPiolicy.java
張貼留言