码迷,mamicode.com
首页 > 移动开发 > 详细

Android 的监听Home键

时间:2015-07-17 13:24:54      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

有使用到,所以就记下来,免得以后会忘记掉

 1 public class HomeListener extends BroadcastReceiver {
 2     private static final String LOG_TAG = "HomeReceiver";
 3     private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
 4     private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
 5     private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
 6     private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
 7     private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
 8 
 9     @Override
10     public void onReceive(Context context, Intent intent) {
11         String action = intent.getAction();
12         Log.i(LOG_TAG, "onReceive: action: " + action);
13         if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
14             // android.intent.action.CLOSE_SYSTEM_DIALOGS
15             String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
16             Log.i(LOG_TAG, "reason: " + reason);
17 
18             if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
19                 // 短按Home键
20                 Log.i(LOG_TAG, "homekey");
21 
22             }
23             else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
24                 // 长按Home键 或者 activity切换键
25                 Log.i(LOG_TAG, "long press home key or activity switch");
26 
27             }
28             else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
29                 // 锁屏
30                 Log.i(LOG_TAG, "lock");
31             }
32             else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
33                 // samsung 长按Home键
34                 Log.i(LOG_TAG, "assist");
35             }
36 
37         }
38     }
39 }

然后程序中开始位置,注册广播

1 HomeListener mHomeKeyReceiver = new HomeListener();
2  final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
3        this.registerReceiver(mHomeKeyReceiver, homeFilter);

 

Android 的监听Home键

标签:

原文地址:http://www.cnblogs.com/mr-zhang/p/4653926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!