Android中Home键的监听和拦截示例
程序员文章站
2023-12-18 12:14:46
首先大家应该先了解一种情况,就是android在应用中是无法拦截home键的,今天我们带大家看一下home键的三种情况。
1、在应用中按下home键的逻辑处理
当我们在...
首先大家应该先了解一种情况,就是android在应用中是无法拦截home键的,今天我们带大家看一下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); }
好啦,这里就对home键进行简单的监听和拦截。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。