Android Home键监听

来源:互联网 发布:郑州华信学院淘宝地址 编辑:程序博客网 时间:2024/06/04 18:31

1.  在onResum里面注册广播,OnPause里面注销广播。

  2. 再广播中拦截Intent.ACTION_CLOSE_SYSTEM_DIALOGS 这个Action ,通过获取Reason字段 来判断长按 还是单击Home键。

下面看看代码:

[java] view plaincopyprint?
 
  1. package com.way.common.util;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7.   
  8. /** 
  9.  * Home键监听封装 
  10.  *  
  11.  * @author way 
  12.  *  
  13.  */  
  14. public class HomeWatcher {  
  15.   
  16.     static final String TAG = "HomeWatcher";  
  17.     private Context mContext;  
  18.     private IntentFilter mFilter;  
  19.     private OnHomePressedListener mListener;  
  20.     private InnerRecevier mRecevier;  
  21.   
  22.     // 回调接口  
  23.     public interface OnHomePressedListener {  
  24.         public void onHomePressed();  
  25.   
  26.         public void onHomeLongPressed();  
  27.     }  
  28.   
  29.     public HomeWatcher(Context context) {  
  30.         mContext = context;  
  31.         mRecevier = new InnerRecevier();  
  32.         mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
  33.     }  
  34.   
  35.     /** 
  36.      * 设置监听 
  37.      *  
  38.      * @param listener 
  39.      */  
  40.     public void setOnHomePressedListener(OnHomePressedListener listener) {  
  41.         mListener = listener;  
  42.     }  
  43.   
  44.     /** 
  45.      * 开始监听,注册广播 
  46.      */  
  47.     public void startWatch() {  
  48.         if (mRecevier != null) {  
  49.             mContext.registerReceiver(mRecevier, mFilter);  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 停止监听,注销广播 
  55.      */  
  56.     public void stopWatch() {  
  57.         if (mRecevier != null) {  
  58.             mContext.unregisterReceiver(mRecevier);  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * 广播接收者 
  64.      */  
  65.     class InnerRecevier extends BroadcastReceiver {  
  66.         final String SYSTEM_DIALOG_REASON_KEY = "reason";  
  67.         final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";  
  68.         final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";  
  69.         final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";  
  70.   
  71.         @Override  
  72.         public void onReceive(Context context, Intent intent) {  
  73.             String action = intent.getAction();  
  74.             if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {  
  75.                 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);  
  76.                 if (reason != null) {  
  77.                     L.i(TAG, "action:" + action + ",reason:" + reason);  
  78.                     if (mListener != null) {  
  79.                         if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {  
  80.                             // 短按home键  
  81.                             mListener.onHomePressed();  
  82.                         } else if (reason  
  83.                                 .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {  
  84.                             // 长按home键  
  85.                             mListener.onHomeLongPressed();  
  86.                         }  
  87.                     }  
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92. }  
 

然后再Activity中重写OnResum方法

 

[java] view plaincopyprint?
 
  1. @Override  
  2. protected void onResume() {  
  3.     // TODO Auto-generated method stub  
  4.     mHomeWatcher = new HomeWatcher(this);  
  5.     mHomeWatcher.setOnHomePressedListener(this);  
  6.     mHomeWatcher.startWatch();  
  7.     super.onResume();  
  8. }  


再Onpause方法里面注销监听

[java] view plaincopyprint?
 
  1. @Override  
  2. protected void onPause() {  
  3.     // TODO Auto-generated method stub  
  4.     super.onPause();  
  5.     mHomeWatcher.setOnHomePressedListener(null);  
  6.     mHomeWatcher.stopWatch();  
  7. }  

在Activity中实现OnHomePressedListener接口,我们就可以在实现方法里面做我们自己的操作了

 

 

转载自:http://blog.csdn.net/chenchuntong/article/details/16824101

0 0
原创粉丝点击