home鍵在KeyEvent中的鍵值為3.
ublic static final int KEYCODE_HOME = 3;
當用戶按下home鍵的時候(包括長按),程序會進入到PhoneWindowManager.java類中的public boolean interceptKeyBeforeDispatching(WindowState win, int action, int flags,int keyCode, int scanCode, int metaState, int repeatCount, int policyFlags)這個方法中進行處理。如果用戶是連續點擊home,此時就要執行長按home事件了。
即執行mHandler.postDelayed(mHomeLongPress,ViewConfiguration.getGlobalActionKeyTimeout());對應的代碼。也就會跳轉到mHomeLongPress這個Runnable接著往下執行。
interceptKeyBeforeDispatching這個方法位於PhoneWindowManager.java中。
位置為:\frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java ?
public?boolean?interceptKeyBeforeDispatching(WindowState?win,?int?action,?int?flags,?int?keyCode,?int?scanCode,?int?metaState,?int?repeatCount,?int?policyFlags)?{?
final?boolean?down?=?(action?==?KeyEvent.ACTION_DOWN);?
...?
//4、用戶按下home,然後馬上釋放。此時這個條件成立。將之前postDelayed的事件remove掉。此時就不會執行長按home事件。?
if?((keyCode?==?KeyEvent.KEYCODE_HOME)?&&?!down)?{?
mHandler.removeCallbacks(mHomeLongPress);?
}?
//5、第壹次按下home,mHomePressed為false。?
if?(mHomePressed)?{?
if?(keyCode?==?KeyEvent.KEYCODE_HOME)?{?
//a、如果用戶連續按下home,此時暫時沒有up事件。所以就不走這裏。?
//b、如果用戶沒有連續按下home,此時過來的是up(move或者/)事件。即!down為true,執行該方法?
if?(!down)?{?
mHomePressed?=?false;?
if?(!canceled)?{?
boolean?incomingRinging?=?false;?
try?{?
ITelephony?telephonyService?=?getTelephonyService();?
if?(telephonyService?!=?null)?{?
incomingRinging?=?telephonyService.isRinging();?
}?
}?catch?(RemoteException?ex)?{?
Log.w(TAG,?"RemoteException?from?getPhoneInterface()",?ex);?
}?
if?(incomingRinging)?{?
Log.i(TAG,?"Ignoring?HOME;?there's?a?ringing?incoming?call.");?
}?else?{?
//單擊home處理?
launchHomeFromHotKey();?
}?
}?else?{?
Log.i(TAG,?"Ignoring?HOME;?event?canceled.");?
}?
}?
}?
return?true;?
}?
...?
//?1、第壹次處理home按下?
if?(keyCode?==?KeyEvent.KEYCODE_HOME)?{?
//?If?a?system?window?has?focus,?then?it?doesn't?make?sense?
//?right?now?to?interact?with?applications.?
WindowManager.LayoutParams?attrs?=?win?!=?nullwin.getAttrs()?:?null;?
if?(attrs?!=?null)?{?
final?int?type?=?attrs.type;?
if?(type?==?WindowManager.LayoutParams.TYPE_KEYGUARD?
||?type?==?WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG)?{?
//?the?"app"?is?keyguard,?so?give?it?the?key?
return?false;?
}?
final?int?typeCount?=?WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;?
for?(int?i=0;?i<typeCount;?i++)?{?
if?(type?==?WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i])?{?
//?don't?do?anything,?but?also?don't?pass?it?to?the?app?
return?true;?
}?
}?
}?
//?2、第壹次按下home,會調用postDelayed發送壹個延時處理的操作。同時將mHomePressed置為true。?
//?如果第5?步沒有進入if?(!down),此時就要執行長按home方法了。?
if?(down?&&?repeatCount?==?0)?{?
if?(!keyguardOn)?{?
mHandler.postDelayed(mHomeLongPress,?ViewConfiguration.getGlobalActionKeyTimeout());?
}?
mHomePressed?=?true;?
}?
return?true;?
}?//其他鍵的處理?
else?if(...){...}