android的Home键的监听封装:

 1 package com.gzcivil.utils; 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  */12 public class HomeListener {13 14     static final String TAG = "HomeListener";15     private Context mContext;16     private IntentFilter mFilter;17     private OnHomePressedListener mListener;18     private InnerRecevier mRecevier;19 20     // 回调接口21     public interface OnHomePressedListener {22         public void onHomePressed();23         public void onHomeLongPressed();24     }25 26     public HomeListener(Context context) {27         mContext = context;28         mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);29     }30 31     /**32      * 设置监听33      * 34      * @param listener35      */36     public void setOnHomePressedListener(OnHomePressedListener listener) {37         mListener = listener;38         mRecevier = new InnerRecevier();39     }40 41     /**42      * 开始监听,注册广播43      */44     public void startWatch() {45         if (mRecevier != null) {46             mContext.registerReceiver(mRecevier, mFilter);47         }48     }49 50     /**51      * 停止监听,注销广播52      */53     public void stopWatch() {54         if (mRecevier != null) {55             mContext.unregisterReceiver(mRecevier);56         }57     }58 59     /**60      * 广播接收者61      */62     class InnerRecevier extends BroadcastReceiver {63         final String SYSTEM_DIALOG_REASON_KEY = "reason";64         final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";65         final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";66         final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";67 68         @Override69         public void onReceive(Context context, Intent intent) {70             String action = intent.getAction();71             if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {72                 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);73                 if (reason != null) {74                     // Log.e(TAG, "action:" + action + ",reason:" + reason);75                     if (mListener != null) {76                         if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {77                             // 短按home键78                             mListener.onHomePressed();79                         } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {80                             // 长按home键81                             mListener.onHomeLongPressed();82                         }83                     }84                 }85             }86         }87     }88 }

更多相关文章

  1. Android中Adapter接口及其实现类详解
  2. Android day_10-02 (广播接收者的使用 五个小案例)
  3. Android Asynchronous Http Client-Android异步网络请求客户端接
  4. android 如何使用Parcelable接口
  5. Android接口回调总结,以及运用到弹窗PopWindow的Demo实现
  6. Android 匿名共享内存C接口分析
  7. Android PinyinIME 源码笔记 -- 1. 底层服务接口简介
  8. Android App怎样调用 Frameworks Bluetooth接口

随机推荐

  1. Android程式编写及调试新手入门-3
  2. android中判断关于文本展示是否出现省略
  3. Android(安卓)4.4 Fence在SurfaceFlinger
  4. Android(安卓)实现item可左右滑动移除的G
  5. Android(安卓)高仿微信发朋友圈浏览图片
  6. Android(安卓)属性动画代码分析(基于Obje
  7. android webview内嵌网页js解析json 并根
  8. Android无缝切换主题,动态换肤
  9. 如何取得Android(安卓)系统的源代码--全
  10. 挑战独立开发项目能力--IT蓝豹