您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android應用中如何監聽與攔截Home鍵,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1、在應用中按下Home鍵的邏輯處理
當我們在應用中按下Home鍵時界面會啟動到桌面,我們在frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.Java類中可以看到其實現原理,其不外乎就是調用了以下代碼。
Intent mHomeIntent; mHomeIntent = new Intent(Intent.ACTION_MAIN, null); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); startActivity(mHomeIntent);
創建一個啟動到桌面的Intent。
2、在應用中監聽Home鍵
在Android應用中如果想監聽Home鍵可以使用廣播機制,這個在源碼中也有體現。
static public final String SYSTEM_DIALOG_REASON_KEY = "reason"; static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"; static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; static public final String SYSTEM_DIALOG_REASON_ASSIST = "assist"; @Override public void onReceive(Context arg0, Intent arg1) { String action = arg1.getAction(); //按下Home鍵會發送ACTION_CLOSE_SYSTEM_DIALOGS的廣播 if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = arg1.getStringExtra(SYSTEM_DIALOG_REASON_KEY); if (reason != null) { if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { // 短按home鍵 Toast.makeText(arg0, "短按Home鍵", Toast.LENGTH_SHORT).show(); } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { // RECENT_APPS鍵 Toast.makeText(arg0, "RECENT_APPS", Toast.LENGTH_SHORT).show(); } } } }
這樣就可以監聽Home的是否被按下。
3、在Frameworks層攔截Home鍵
在frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java文件中我們首先看一下interceptKeyBeforeDispatching()方法。
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) { //...... if (keyCode == KeyEvent.KEYCODE_HOME) { //...... handleShortPressOnHome(); } } //進入handleShortPressOnHome private void handleShortPressOnHome() { // If there's a dream running then use home to escape the dream // but don't actually go home. if (mDreamManagerInternal != null && mDreamManagerInternal.isDreaming()) { mDreamManagerInternal.stopDream(false /*immediate*/); return; } // Go home! launchHomeFromHotKey(); }
進入launchHomeFromHotKey方法。
static public final String SYSTEM_DIALOG_REASON_KEY = "reason"; static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"; static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; static public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; static public final String SYSTEM_DIALOG_REASON_ASSIST = "assist"; void launchHomeFromHotKey() { if (isKeyguardShowingAndNotOccluded()) { // don't launch home if keyguard showing } else if (!mHideLockScreen && mKeyguardDelegate.isInputRestricted()) { // when in keyguard restricted mode, must first verify unlock // before launching home mKeyguardDelegate.verifyUnlock(new OnKeyguardExitResult() { @Override public void onKeyguardExitResult(boolean success) { if (success) { try { ActivityManagerNative.getDefault().stopAppSwitches(); } catch (RemoteException e) { } sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); startDockOrHome(); } } }); } else { // no keyguard stuff to worry about, just launch home! try { ActivityManagerNative.getDefault().stopAppSwitches(); } catch (RemoteException e) { } if (mRecentsVisible) { // Hide Recents and notify it to launch Home awakenDreams(); sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); hideRecentApps(false, true); } else { // Otherwise, just launch Home sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY); //啟動Launcher界面 startDockOrHome(); } } }
以上方法可處理Home鍵的攔截操作,接下來我們進入startDockOrHome方法。
void startDockOrHome() { if (OptConfig.LC_RAM_SUPPORT) { try { ActivityManagerNative.getDefault().startHomePre(); } catch (RemoteException re) { } } awakenDreams(); Intent dock = createHomeDockIntent(); if (dock != null) { try { startActivityAsUser(dock, UserHandle.CURRENT); return; } catch (ActivityNotFoundException e) { } } //intent的相關設置 mHomeIntent = new Intent(Intent.ACTION_MAIN, null); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); startActivityAsUser(mHomeIntent, UserHandle.CURRENT); }
關于Android應用中如何監聽與攔截Home鍵就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。