Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android之Home鍵監聽封裝

Android之Home鍵監聽封裝

編輯:關於Android編程

眾所周知,我們監聽返回鍵事件,無法是下面兩個方法:[java] view plaincopyprint?
@Override 
    public void onBackPressed() { 
        //do something  
        //super.onBackPressed();  
    } 
     
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if(keyCode == KeyEvent.KEYCODE_BACK){ 
            //do something  
        } 
        return super.onKeyDown(keyCode, event); 
    } 

@Override
 public void onBackPressed() {
  //do something
  //super.onBackPressed();
 }
 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if(keyCode == KeyEvent.KEYCODE_BACK){
   //do something
  }
  return super.onKeyDown(keyCode, event);
 }


於是乎,我們理所當然的想到,監聽Home鍵的方法是:if(keyCode == KeyEvent.KEYCODE_HOME)。但實際上,這樣是行不通的,因為此時home鍵的消息在framework層就已經被攔截,所以,我們在應用中通過此方法是無法監聽到Home鍵的消息的。那麼我們如何捕獲Home鍵事件,並作出相應處理呢?其實系統還是給我們發出廣播的,下面,就跟大家分享一下,封裝好了的監聽Home鍵的方法:

HomeWatcher類:

 

[java]
/**
 * Home鍵監聽封裝
 * 
 * @author way
 * 
 */ 
public class HomeWatcher { 
 
    static final String TAG = "HomeWatcher"; 
    private Context mContext; 
    private IntentFilter mFilter; 
    private OnHomePressedListener mListener; 
    private InnerRecevier mRecevier; 
 
    // 回調接口  
    public interface OnHomePressedListener { 
        public void onHomePressed(); 
 
        public void onHomeLongPressed(); 
    } 
 
    public HomeWatcher(Context context) { 
        mContext = context; 
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
    } 
 
    /**
     * 設置監聽
     * 
     * @param listener
     */ 
    public void setOnHomePressedListener(OnHomePressedListener listener) { 
        mListener = listener; 
        mRecevier = new InnerRecevier(); 
    } 
 
    /**
     * 開始監聽,注冊廣播
     */ 
    public void startWatch() { 
        if (mRecevier != null) { 
            mContext.registerReceiver(mRecevier, mFilter); 
        } 
    } 
 
    /**
     * 停止監聽,注銷廣播
     */ 
    public void stopWatch() { 
        if (mRecevier != null) { 
            mContext.unregisterReceiver(mRecevier); 
        } 
    } 
 
    /**
     * 廣播接收者
     */ 
    class InnerRecevier extends BroadcastReceiver { 
        final String SYSTEM_DIALOG_REASON_KEY = "reason"; 
        final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"; 
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; 
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; 
 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); 
                if (reason != null) { 
                    Log.e(TAG, "action:" + action + ",reason:" + reason); 
                    if (mListener != null) { 
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { 
                            // 短按home鍵  
                            mListener.onHomePressed(); 
                        } else if (reason 
                                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { 
                            // 長按home鍵  
                            mListener.onHomeLongPressed(); 
                        } 
                    } 
                } 
            } 
        } 
    } 

/**
 * Home鍵監聽封裝
 *
 * @author way
 *
 */
public class HomeWatcher {

 static final String TAG = "HomeWatcher";
 private Context mContext;
 private IntentFilter mFilter;
 private OnHomePressedListener mListener;
 private InnerRecevier mRecevier;

 // 回調接口
 public interface OnHomePressedListener {
  public void onHomePressed();

  public void onHomeLongPressed();
 }

 public HomeWatcher(Context context) {
  mContext = context;
  mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
 }

 /**
  * 設置監聽
  *
  * @param listener
  */
 public void setOnHomePressedListener(OnHomePressedListener listener) {
  mListener = listener;
  mRecevier = new InnerRecevier();
 }

 /**
  * 開始監聽,注冊廣播
  */
 public void startWatch() {
  if (mRecevier != null) {
   mContext.registerReceiver(mRecevier, mFilter);
  }
 }

 /**
  * 停止監聽,注銷廣播
  */
 public void stopWatch() {
  if (mRecevier != null) {
   mContext.unregisterReceiver(mRecevier);
  }
 }

 /**
  * 廣播接收者
  */
 class InnerRecevier extends BroadcastReceiver {
  final String SYSTEM_DIALOG_REASON_KEY = "reason";
  final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
  final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
  final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

  @Override
  public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
    String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
    if (reason != null) {
     Log.e(TAG, "action:" + action + ",reason:" + reason);
     if (mListener != null) {
      if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
       // 短按home鍵
       mListener.onHomePressed();
      } else if (reason
        .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
       // 長按home鍵
       mListener.onHomeLongPressed();
      }
     }
    }
   }
  }
 }
}

 

 

接下來,我們需要在應用中開啟監聽,下面是以在Activity中的應用為例:


[java] 
/**
 * 應用HomeWatcher小例
 * 
 * @author way
 * 
 */ 
public class MainActivity extends Activity { 
    private static final String TAG = "MainActivity"; 
    private HomeWatcher mHomeWatcher; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        mHomeWatcher = new HomeWatcher(this); 
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() { 
            @Override 
            public void onHomePressed() { 
                Log.e(TAG, "onHomePressed"); 
            } 
 
            @Override 
            public void onHomeLongPressed() { 
                Log.e(TAG, "onHomeLongPressed"); 
            } 
        }); 
        mHomeWatcher.startWatch(); 
    } 
 
    @Override 
    protected void onPause() { 
        super.onPause(); 
        mHomeWatcher.stopWatch();// 在onPause中停止監聽,不然會報錯的。  
    } 

/**
 * 應用HomeWatcher小例
 *
 * @author way
 *
 */
public class MainActivity extends Activity {
 private static final String TAG = "MainActivity";
 private HomeWatcher mHomeWatcher;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mHomeWatcher = new HomeWatcher(this);
  mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
   @Override
   public void onHomePressed() {
    Log.e(TAG, "onHomePressed");
   }

   @Override
   public void onHomeLongPressed() {
    Log.e(TAG, "onHomeLongPressed");
   }
  });
  mHomeWatcher.startWatch();
 }

 @Override
 protected void onPause() {
  super.onPause();
  mHomeWatcher.stopWatch();// 在onPause中停止監聽,不然會報錯的。
 }
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved