ITeye博客

Android Home键监听

Java-Object · 2014-01-13 15:02 · Android技术 · 编程语言 · Markdown

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

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

下面看看代码:

[java] view plaincopyprint?

 

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

 

然后再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)

暂无评论。

评论功能维护中,当前仅展示历史评论。