編輯:關於Android編程
一:Android處理Power按鍵長按操作
在Framework層中,Android4.x對Power鍵(KeyEvent.KEYCODE_POWER)的操作,我們從PhoneWindowManager開始分析,在分析前我這裡打印了該方法的堆棧調用信息。大家可以參考一下。
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
......
android.util.Log.d("BILL",android.util.Log.getStackTraceString(new Throwable()));
......
}
1-13 19:35:32.458 D/BILL ( 718): java.lang.Throwable
01-13 19:35:32.458 D/BILL ( 718): at com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching(PhoneWindowManager.java:2224)
01-13 19:35:32.458 D/BILL ( 718): at com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching(InputMonitor.java:501)
01-13 19:35:32.458 D/BILL ( 718): at com.android.server.input.InputManagerService.interceptKeyBeforeDispatching(InputManagerService.java:1383)
01-13 19:35:32.458 D/BILL ( 718): at dalvik.system.NativeStart.run(Native Method)
調用流程如下(只貼出關鍵代碼):
interceptKeyBeforeDispatching()-->interceptPowerKeyDown()-->mPowerLongPress.run()
1>
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
......
case KeyEvent.KEYCODE_POWER: {
result &= ~ACTION_PASS_TO_USER;
if (down) {
mImmersiveModeConfirmation.onPowerKeyDown(isScreenOn, event.getDownTime(),
isImmersiveMode(mLastSystemUiFlags));
if (isScreenOn && !mPowerKeyTriggered
&& (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
mPowerKeyTriggered = true;
mPowerKeyTime = event.getDownTime();
interceptScreenshotChord();
}
ITelephony telephonyService = getTelephonyService();
boolean hungUp = false;
if (telephonyService != null) {
try {
if (telephonyService.isRinging()) {
// Pressing Power while there's a ringing incoming
// call should silence the ringer.
telephonyService.silenceRinger();
/// M: [ALPS00093981] @{
} else if ((isScreenOn
|| mScreenOffReason == OFF_BECAUSE_OF_PROX_SENSOR)
/// @}
&& (mIncallPowerBehavior
& Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0
&& telephonyService.isOffhook()) {
// Otherwise, if "Power button ends call" is enabled,
// the Power button will hang up any current active call.
hungUp = telephonyService.endCall();
}
} catch (RemoteException ex) {
Log.w(TAG, "ITelephony threw RemoteException", ex);
}
}
interceptPowerKeyDown(!isScreenOn || hungUp
|| mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);
} else {
mPowerKeyTriggered = false;
cancelPendingScreenshotChordAction();
if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {
result = (result & ~ACTION_WAKE_UP) | ACTION_GO_TO_SLEEP;
}
mPendingPowerKeyUpCanceled = false;
}
break;
}
......
}
注!紅色為判斷長按(down),藍色為判斷短按(up)。
2>
private void interceptPowerKeyDown(boolean handled) {
mPowerKeyHandled = handled;
if (!handled) {
mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
3>
private final Runnable mPowerLongPress = new Runnable() {
......
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(resolvedBehavior == LONG_PRESS_POWER_SHUT_OFF);
break;
......
};
注:上述代碼中下劃線即彈出(關機、重啟、飛行模式等選項)的對話框。
程序應用步驟:打開應用:onCreateonStartonResumeBACK鍵:onPauseonStoponDestoryHOME鍵:onPauseonStop再次啟
一、TabHost用法通常情況下我們會通過繼承TabActivity,調用getTabHost()獲取TabHost實例,下面是具體過程。TabHostActivity.
Android的啟動過程是從init開始的,它是所有後續進程的祖進程。系統啟動的過程可以大致分為以下幾個步驟1,init.c的啟動 掛載目錄 初始化 解析配置文件2
最近在寫一個小項目,其中有一點用到了顯示EditText中輸入了多少個字符,像微博中顯示剩余多少字符的功能。在EditText提供了一個方法addTextChangedL