ifflag this.getWindow.setTypeWindowManager.LayoutParams.TYPE_KEYGUARD super.onAttachedToWindow Overridepublic boolean onKeyDownint keyCode KeyEvent event ifkeyCode KeyEvent.KEYCODE_HOME return true returnsuper.onKeyDownkeyCode event Step3: 真正的原因如下,对锁屏模式的处理: // If a system window has focus then it doesnt make sense //right now to interact with applications. WindowManager.LayoutParamsattrs win null win.getAttrs : null if attrs null final int type attrs.type if type WindowManager.LayoutParams.TYPE_KEYGUARD type WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG // thequotappquot is keyguard so give it the key return 0 final int typeCount WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.lengthfor int i0 ilttypeCount i if type WINDOW_TYPES_WHERE_HOME_DOESNT_WORKi // dont doanything but also dont pass it to the app return-1 Step4: 我们来看点击 home 键,为什么进入到 launcher 的待机界面: // If we have released the home key and didnt do anything else// while it was pressed then it is time to go home if mHomePressedampamp down mHomePressed false if canceled // If an incoming call is ringing HOME is totally disabled.// The user is already on the InCallScreen at this point // andhis ONLY options are to answer or reject the call. booleanincomingRinging false try ITelephony telephonyService getTelephonyServiceif telephonyService null incomingRinging telephonyService.isRinging catch RemoteException ex Log.wTAG quotRemoteExceptionfrom getPhoneInterfacequot ex ifincomingRinging Log.iTAG quotIgnoring HOME theres aringing incoming call.quot else launchHomeFromHotKey else Log.iTAG quotIgnoring HOME eventcanceled.quot return -1 Step5: 系统会判断,当前点击 Home 键并且没有电话打入的情况,才对 Home 键进行处理----gtlaunchHomeFromHotKey进入到-----gtlaunchHomeFromHotKey方法中: / A home key -gt launch home action was detected. Take the appropriateaction given the situation with the keyguard. / voidlaunchHomeFromHotKey if mKeyguardMediator.isShowingAndNotHidden // dont launch home if keyguard showing else ifmHideLockScreen ampamp mKeyguardMediator.isInputRestricted // whenin keyguard restricted mode must first verify unlock // before launchinghome mKeyguardMediator.verifyUnlocknew OnKeyguardExitResult public void onKeyguardExitResultboolean success if success try ActivityManagerNative.getDefault.stopAppSwitches catch RemoteException e sendCloseSystemWindowsSYSTEM_DIALOG_REASON_HOME_KEYstartDockOrHome else // no keyguard stuff to worry about just launch hometry ActivityManagerNative.getDefault.stopAppSwitches catch RemoteException e sendCloseSystemWindowsSYSTEM_DIALOG_REASON_HOME_KEYstartDockOrHome Step6: 也 是 对 锁 屏 模 式 有 个 判 断 , 如 果 不 在 锁 屏 模 式 , 就 launchHome----gtstartDockOrHome进入——gt void startDockOrHome Intent dock createHomeDockIntent ifdock null try mContext.startActivitydockreturn catch ActivityNotFoundException e mContext.startActivitymHomeIntent Step 7: 其实这个 createHomeDockIntent()方法就是对 android 手机的几种模式进行判断,The device is not in either car mode or desk mode The device is in car mode butENABLE_CAR_DOCK_HOME_CAPTURE is false The device is in desk mode butENABLE_DESK_DOCK_HOME_CAPTURE is false The device is in car mode but theres noCAR_DOCK app with METADATA_DOCK_HOME The device is in desk mode but theres noDESK_DOCK app with METADATA_DOCK_HOME如果是以上模式,车载模式或者桌面模式,就返回 dock 不为空,否则为空。
启动这个mHomeIntent。
-----gtmHomeIntent 定义如下: mHomeIntent new IntentIntent.ACTION_MAIN nullmHomeIntent.addCategoryIntent.CATEGORY_HOMEmHomeIntent.addFlagsIntent.FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED Step 8: 这 个 mHomeIntent 就 是 启 动activity 中配置 Category 属性的值为 CATEGORY_HOME,启动的时候新启动一个任务,不是在当前的这个任务中启动 launcherHome,而是新建一个 task。
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 标记进入前台时(典型的操作是用户在主画面重启它),这个 Activity 和它之上的都将关闭,以至于用户不能再返回到它们,但是可以回到之前的 Activity。
到这为止,Home 键的流程已经分析完了。
二 、下面看看长按耳机键接受的广播的处理方式:(1)这个长按耳机键捕获是在 PhoneWindow.java 类的 onKeyDown中,然后发送有序的广播----gt如下: case KeyEvent.KEYCODE_HEADSETHOOK: caseKeyEvent.KEYCODE_MEDIA_STOP: case KeyEvent.KEYCODE_MEDIA_NEXT:case KeyEvent.KEYCODE_MEDIA_PREVIOUS: caseKeyEvent.KEYCODE_MEDIA_REWIND: caseKeyEvent.KEYCODE_MEDIA_FAST_FORWARD: Intent intent newIntentIntent.ACTION_MEDIA_BUTTON nullintent.putExtraIntent.EXTRA_KEY_EVENT eventgetContext.sendOrderedBroadcastintent null returntrue 接受这个长按耳机键的广播是在 Music 的 app 中的-----gt publicclassMediaButtonIntentReceiverextendsBroadcastReceiver...... , 需要在 Manifest.xml 中注册这个广播ltintent-filtergt ltaction android:namequotandroid.intent.action.MEDIA_BUTTONquot /gt lt/intent-filtergt。
----gt注册 MediaButtonReceiver 这个广播, 这个类中 onReceive()方法定义的:代码如下---gt Override public void onReceiveContext context Intent intent String intentAction intent.getAction ifAudioManager.ACTION_AUDIO_BECOMING_NOISY.equalsintentAction Intent i new IntentMediaPlaybackService.SERVICECMDi.putExtraMediaPlaybackService.CMDNAME MediaPlaybackService.CMDPAUSEcontext.sendBroadcasti else ifIntent.ACTION_MEDIA_BUTTON.equalsintentAction KeyEvent event KeyEvent intent.getParcelableExtraIntent.EXTRA_KEY_EVENTif event null return int keycode event.getKeyCode int action event.getAction longeventtime event.getEventTime // single quick press: pause/resume.// double press: next track // long press: s.
上一篇:
Android
下一篇:
试题库管理系统毕业论文