Android Home键监听的实现代码

时间:2021-07-10 07:13:22

之前的一个项目需要监听home键的事件,发现用dispatchkeyevent无法实现监听,后来查了些资料,才知道home键不能用常规的按键事件监听,需要使用广播的方法。下面是我封装的一个工具类。根据自己实际需要修改吧。

注册广播:

?
1
homelistener.getinstance().start(this);

移除广播

?
1
homelistener.getinstance().stop(this);

设置监听回调

?
1
homelistener.getinstance().sethomekeylistener(homepresslistener listener);

添加监听回调

?
1
homelistener.getinstance().addhomekeylistener(homepresslistener listener)

移除监听回调

?
1
homelistener.getinstance().removehomekeylistener(homepresslistener listener);

移除所有监听回调

?
1
homelistener.getinstance().removeallhomekeylistener();

销毁

?
1
homelistener.getinstance().destroy();

工具类代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
 
import java.util.arraylist;
import java.util.list;
import java.util.concurrent.atomic.atomicboolean;
 
public class homelistener {
 public static homekeylistener getinstance() {
  homekeylistener listener = homekeylistener.slistener;
  listener.init();
  return listener;
 }
 
 static class homekeylistener {
  private static final string system_dialog_reason_recent_apps = "recentapps";//home键旁边的最近程序列表键
  private static final string system_dialog_reason_home_key = "homekey";//按下home键
  private static final string system_dialog_reason_assist = "assist";//某些三星手机的程序列表键
 
  private static atomicboolean isdestroy = new atomicboolean(true);
  private static atomicboolean isregister = new atomicboolean(false);
  private static homekeylistener slistener = new homekeylistener();
  private list<homepresslistener> mpresslisteners = new arraylist<homepresslistener>();
  private homereceiver mreceiver;
  private intentfilter mhomefileter;
  private homepresslistener mhomepresslistener;
  public void init() {
   if (isdestroy.get()) {
    this.mpresslisteners = new arraylist<homepresslistener>();
    this.mreceiver = new homereceiver();
    this.mhomefileter = new intentfilter(intent.action_close_system_dialogs);
    isdestroy.set(false);
   }
  }
 
  public synchronized void start(context context) {
   if (!isregister.get()) {
    context.registerreceiver(mreceiver, mhomefileter);
    isregister.set(true);
   }
  }
 
  public synchronized void stop(context context) {
   if (isregister.get()) {
    context.unregisterreceiver(mreceiver);
    isregister.set(false);
   }
  }
 
  public void sethomekeylistener(homepresslistener listener) {
   this.mhomepresslistener = listener;
  }
 
  public void addhomekeylistener(homepresslistener listener) {
   mpresslisteners.add(listener);
  }
 
  public void removehomekeylistener(homepresslistener listener) {
   mpresslisteners.add(listener);
  }
 
  public void removeallhomekeylistener() {
   mpresslisteners.clear();
  }
 
  public void destroy() {
   this.mpresslisteners.clear();
   this.mpresslisteners = null;
   this.mreceiver = null;
   this.mhomefileter = null;
   this.mhomepresslistener = null;
   isdestroy.set(true);
  }
 
  public interface homepresslistener {
   void onhomepress();
   void onhomerecentappspress();
  }
 
  class homereceiver extends broadcastreceiver {
   @override
   public void onreceive(context context, intent intent) {
    receive(intent);
   }
  }
 
  private void receive(intent intent) {
   string action = intent.getaction();
   if (action != null && action.equals(intent.action_close_system_dialogs)) {
    string reason = intent.getstringextra( "reason" );
    dispatchpress(reason);
   }
  }
 
  private void dispatchpress(string reason) {
   switch (reason) {
    case system_dialog_reason_home_key:
     if (mhomepresslistener != null) mhomepresslistener.onhomepress();
     for (homepresslistener listener : mpresslisteners) listener.onhomepress();
     break;
 
    case system_dialog_reason_recent_apps:
    case system_dialog_reason_assist:
     if (mhomepresslistener != null) mhomepresslistener.onhomerecentappspress();
     for (homepresslistener listener : mpresslisteners) listener.onhomerecentappspress();
     break;
 
   }
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/ad937a3ca243