代码:

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

 

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2021-04-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-01-16
  • 2021-09-16
猜你喜欢
  • 2022-12-23
  • 2021-05-19
  • 2022-01-28
  • 2022-12-23
  • 2021-12-22
相关资源
相似解决方案