1. 程式人生 > >Android Back Home鍵監聽

Android Back Home鍵監聽

Android Back Home鍵監聽

 

Android Back Home鍵監聽

 

Back鍵的監聽

  對於Back鍵的監聽比較容易,可以在多個系統回撥處攔截,比如在activity的下列方法中都可以收到Back鍵按下的事件:

@Override

    public void onBackPressed() {

        // super.onBackPressed();//註釋掉這行,back鍵不退出activity

        Log.i(LOG_TAG, "onBackPressed");

    }

    @Override

    public boolean dispatchKeyEvent(KeyEvent event) {

        Log.i(LOG_TAG, "dispatchKeyEvent: keyCode -- " + event.getKeyCode());

        return super.dispatchKeyEvent(event);

    }



    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        Log.i(LOG_TAG, "onKeyDown: keyCode -- " + keyCode);

        switch (keyCode) {

        case KeyEvent.KEYCODE_BACK:

            Log.i(LOG_TAG, "KeyEvent.KEYCODE_BACK");

            break;

        case KeyEvent.KEYCODE_MENU:

            Log.i(LOG_TAG, "KeyEvent.KEYCODE_MENU");

            break;

        case KeyEvent.KEYCODE_HOME:

            Log.i(LOG_TAG, "KeyEvent.KEYCODE_HOME");

            // 收不到

            break;

        case KeyEvent.KEYCODE_APP_SWITCH:

            Log.i(LOG_TAG, "KeyEvent.KEYCODE_APP_SWITCH");

            // 收不到

            break;

        default:

            break;

        }



        return super.onKeyDown(keyCode, event);



    }

 

 

Home鍵的廣播監聽

  對於Home鍵的監聽不是那麼容易,因為Home鍵可以將程式退出放在後臺,所以這個事件是直接分發給系統,系統接收到之後做相應處理,Home鍵的事件不是直接傳遞到應用裡面.所以在上述監聽Back鍵的程式碼中,相應的回撥中是收不到Home鍵的事件的.

  參考文後的部落格連結,對Home鍵的監聽主要通過註冊廣播接收器實現,攔截讓視窗關閉的系統動作,然後根據Intent裡面的具體引數,分析當前到底是Home鍵, 應用切換鍵,還是其他功能按鍵.

  接收器實現如下:

package com.mengdd.hellohome;



import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;



public class HomeWatcherReceiver extends BroadcastReceiver {

    private static final String LOG_TAG = "HomeReceiver";

    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";

    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";

    private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";



    @Override

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        Log.i(LOG_TAG, "onReceive: action: " + action);

        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {

            // android.intent.action.CLOSE_SYSTEM_DIALOGS

            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

            Log.i(LOG_TAG, "reason: " + reason);



            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {

                // 短按Home鍵

                Log.i(LOG_TAG, "homekey");



            }

            else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {

                // 長按Home鍵 或者 activity切換鍵

                Log.i(LOG_TAG, "long press home key or activity switch");



            }

            else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {

                // 鎖屏

                Log.i(LOG_TAG, "lock");

            }

            else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {

                // samsung 長按Home鍵

                Log.i(LOG_TAG, "assist");

            }



        }

    }



}

 

  注意不同手機的按鍵不同,所以需要對不同理由做區分.

 

Home鍵監聽廣播註冊

  廣播接收器的註冊有兩種方式,一種是靜態註冊,即寫在manifest裡面宣告;另一種是動態註冊,即在Java程式碼裡面註冊.

  上面對Home鍵實現監聽的這個receiver,靜態註冊如下:

<receiver android:name="com.mengdd.hellohome.HomeWatcherReceiver" > <intent-filter> <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS"

/> </intent-filter> </receiver>

  但是發現靜態註冊不起作用,即收不到onReceive回撥.

 

  採用動態註冊:

private static HomeWatcherReceiver mHomeKeyReceiver = null;



    private static void registerHomeKeyReceiver(Context context) {

        Log.i(LOG_TAG, "registerHomeKeyReceiver");

        mHomeKeyReceiver = new HomeWatcherReceiver();

        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);



        context.registerReceiver(mHomeKeyReceiver, homeFilter);

    }



    private static void unregisterHomeKeyReceiver(Context context) {

        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");

        if (null != mHomeKeyReceiver) {

            context.unregisterReceiver(mHomeKeyReceiver);

        }

    }


  

  在Activity的onResume和onPause裡面分別呼叫:


@Override

    protected void onResume() {

        super.onResume();



        registerHomeKeyReceiver(this);

    }



    @Override

    protected void onPause() {



        unregisterHomeKeyReceiver(this);

        super.onPause();

    }

 

  當然也可以根據需要在其他合適的時機註冊和登出.