Android中Home鍵的監(jiān)聽和攔截示例
首先大家應(yīng)該先了解一種情況,就是Android在應(yīng)用中是無法攔截Home鍵的,今天我們帶大家看一下Home鍵的三種情況。
1、在應(yīng)用中按下Home鍵的邏輯處理
當(dāng)我們?cè)趹?yīng)用中按下Home鍵時(shí)界面會(huì)啟動(dòng)到桌面,我們?cè)趂rameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.Java類中可以看到其實(shí)現(xiàn)原理,其不外乎就是調(diào)用了以下代碼。
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);
創(chuàng)建一個(gè)啟動(dòng)到桌面的Intent。
2、在應(yīng)用中監(jiān)聽Home鍵
在Android應(yīng)用中如果想監(jiān)聽Home鍵可以使用廣播機(jī)制,這個(gè)在源碼中也有體現(xiàn)。
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鍵會(huì)發(fā)送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();
}
}
}
}
這樣就可以監(jiān)聽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();
}
}
//進(jìn)入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();
}
進(jìn)入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);
//啟動(dòng)Launcher界面
startDockOrHome();
}
}
}
以上方法可處理Home鍵的攔截操作,接下來我們進(jìn)入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的相關(guān)設(shè)置
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);
}
好啦,這里就對(duì)Home鍵進(jìn)行簡單的監(jiān)聽和攔截。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Android開發(fā)TextView內(nèi)的文字實(shí)現(xiàn)自動(dòng)換行
這篇文章主要為大家介紹了Android開發(fā)TextView內(nèi)的文字實(shí)現(xiàn)自動(dòng)換行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Kotlin中的handler如何避免內(nèi)存泄漏詳解
Handler,我們已經(jīng)相當(dāng)熟悉了,而且經(jīng)常用得不亦樂乎,但就是因?yàn)樘煜ち耍艜?huì)偶爾被它反捅一刀,血流不止,下面這篇文章主要給大家介紹了關(guān)于Kotlin中handler如何避免內(nèi)存泄漏的相關(guān)資料,需要的朋友可以參考下。2017-12-12
Android Scroll滑動(dòng)效果實(shí)例
這篇文章主要為大家分享了Android Scroll滑動(dòng)效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android開發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法【基于Matrix】
這篇文章主要介紹了Android開發(fā)之圖片旋轉(zhuǎn)功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android基于matrix矩陣操作圖形變換的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09

