本文章主要講述Android智能機上,應用捕獲按鍵的處理。遙控器上按鍵分爲兩種,普通按鍵和熱鍵。普通按鍵就是咱們經常使用的數字鍵,上下左右功能鍵,設置,返回,確認等按鍵。而在android上,對於遙控器的映射中,規定某些按鍵應用是沒法攔截到的,咱們稱此爲熱鍵,如靜音鍵,電源鍵,HOME鍵,直播鍵等等。 java
1. 應用攔截普通按鍵的處理: android
// 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode(); Log.i(TAG, "[test] dispatchKeyEvent event = " + event); if (key == KeyEvent.KEYCODE_VOLUME_DOWN || key == KeyEvent.KEYCODE_VOLUME_UP) { Log.i(TAG, "[test] catch event!! return true! "); return true; } return super.dispatchKeyEvent(event); }
在按鍵的處理過程當中,若應用重寫了dispatchKeyEvent,而且對響應的按鍵進行了處理完後,直接return true,則系統將再也不處理此按鍵的響應。若是不寫return true,super.dispatchKeyEvent(event)將會將按鍵再次交給系統進行處理。 app
---------------------------------------------------------------------------------- ide
2.應用攔截熱鍵的處理: this
[1]在Activity中重寫onAttachedToWindow方法,設置一個0x80000000的flags下去。//僅一個標誌而已 spa
@Override public void onAttachedToWindow() { System.out.println("onAttachedToWindow ............"); this.getWindow().addFlags(0x80000000);// super.onAttachedToWindow(); }
[2]修改phoneWindownManager.java.在相應按鍵的事件派發前進行以下處理:【以home鍵爲例】 code
在進行事件處理前,先獲取當前WindowManager.LayoutParams,經過flags能夠判斷應用是否能夠處理該按鍵消息,若能夠,直接return 0,再也不進行按鍵處理,交留給應用去處理。若是flags判斷應用沒法獲取該按鍵消息,將還會進行系統原有的按鍵處理流程。 事件
if (keyCode == KeyEvent.KEYCODE_HOME){ // If we have released the home key, and didn't do anything else // while it was pressed, then it is time to go home! if (mHomePressed && !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, // and his ONLY options are to answer or reject the call.) boolean incomingRinging = false; try { ITelephony telephonyService = getTelephonyService(); if (telephonyService != null) { incomingRinging = telephonyService.isRinging(); } } catch (RemoteException ex) { Log.w(TAG, "RemoteException from getPhoneInterface()", ex); } if (incomingRinging) { Log.i(TAG, "Ignoring HOME; there's a ringing incoming call."); }else if(mHoldHomePressed == true){ mHoldHomePressed = false; }else{ mHoldHomePressed = false; launchHomeFromHotKey(); } } else { Log.i(TAG, "Ignoring HOME; event canceled."); } return -1; } // If a system window has focus, then it doesn't make sense // right now to interact with applications. WindowManager.LayoutParams attrs = 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) { // the "app" is keyguard, so give it the key return 0; } final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length; for (int i=0; i<typeCount; i++) { if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) { // don't do anything, but also don't pass it to the app return -1; } } /** * 特殊處理,讓應用程序能攔截HOME按鍵 */ final int attrFlags = attrs.flags; if((attrFlags & 0x80000000) != 0) { return 0; } } if (down) { if (repeatCount == 0) { mHomePressed = true; } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) { if (!keyguardOn) { //handleLongPressOnHome(); mHoldHomePressed = true; Intent intentShowRecent = new Intent("hi_action_show_recent"); mContext.sendBroadcast(intentShowRecent); } } } return -1; }
[3]熱鍵在應用中的處理: get
參照普通按鍵的處理流程。重寫dispatchKeyEvent。 直播