---
title: "Android Home键监听"
author: "Java-Object"
date: "2014-01-13T15:02:55Z"
tags: []
categories: ["编程语言"]
slug: "android-home"
draft: false
lastmod: "2014-01-13T15:02:55Z"
id: 2003355
url: "https://hezd.iteye.com/blog/2003355"
---

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

  2. 再广播中拦截Intent.ACTION\_CLOSE\_SYSTEM\_DIALOGS 这个Action ，通过获取Reason字段 来判断长按 还是单击Home键。

下面看看代码：

**\[java]** [view plain](http://blog.csdn.net/chenchuntong/article/details/16824101 "view plain")[copy](http://blog.csdn.net/chenchuntong/article/details/16824101 "copy")[print](http://blog.csdn.net/chenchuntong/article/details/16824101 "print")[?](http://blog.csdn.net/chenchuntong/article/details/16824101 "?")

 

01. package com.way.common.util;
02. import android.content.BroadcastReceiver;
03. import android.content.Context;
04. import android.content.Intent;
05. import android.content.IntentFilter;
06. /\**
07. * Home键监听封装
08. \*
09. * @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 plain](http://blog.csdn.net/chenchuntong/article/details/16824101 "view plain")[copy](http://blog.csdn.net/chenchuntong/article/details/16824101 "copy")[print](http://blog.csdn.net/chenchuntong/article/details/16824101 "print")[?](http://blog.csdn.net/chenchuntong/article/details/16824101 "?")

 

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 plain](http://blog.csdn.net/chenchuntong/article/details/16824101 "view plain")[copy](http://blog.csdn.net/chenchuntong/article/details/16824101 "copy")[print](http://blog.csdn.net/chenchuntong/article/details/16824101 "print")[?](http://blog.csdn.net/chenchuntong/article/details/16824101 "?")

 

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](http://blog.csdn.net/chenchuntong/article/details/16824101)