Skip to content

Commit 229a170

Browse files
author
mj
committed
安卓按键监听器
1 parent d5f2fd9 commit 229a170

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

app/src/main/java/com/mj/android_note/base/AndroidKeyWatcher.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.IntentFilter;
7-
import android.text.TextUtils;
8-
9-
import androidx.annotation.NonNull;
10-
11-
import java.util.List;
12-
import java.util.Map;
137

148
/**
159
* Author : MJ
@@ -36,7 +30,7 @@ public class AndroidKeyWatcher {
3630
* @param context context
3731
* @param iKeyWatcher 监听器
3832
*/
39-
public void register(@NonNull Context context, IKeyWatcher iKeyWatcher) {
33+
public void register(Context context, IKeyWatcher iKeyWatcher) {
4034
this.iKeyWatcher = iKeyWatcher;
4135
registerReceiver(context);
4236
}
@@ -46,7 +40,7 @@ public void register(@NonNull Context context, IKeyWatcher iKeyWatcher) {
4640
*
4741
* @param context context
4842
*/
49-
public void unRegister(@NonNull Context context) {
43+
public void unRegister(Context context) {
5044
this.iKeyWatcher = null;
5145
unRegisterReceiver(context);
5246
}
@@ -56,19 +50,19 @@ public void unRegister(@NonNull Context context) {
5650
*
5751
* @param context context
5852
*/
59-
private void registerReceiver(@NonNull Context context) {
53+
private void registerReceiver(Context context) {
6054
if (keyReceiver == null) {
6155
keyReceiver = new KeyReceiver();
6256
}
6357
// 使用applicationContext,是为了防止activity的内存泄漏
64-
if (context.getApplicationContext() != null) {
58+
if (context != null && context.getApplicationContext() != null) {
6559
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
6660
context.getApplicationContext().registerReceiver(keyReceiver, filter);
6761
}
6862
}
6963

70-
private void unRegisterReceiver(@NonNull Context context) {
71-
if (context.getApplicationContext() != null && keyReceiver != null) {
64+
private void unRegisterReceiver(Context context) {
65+
if (context != null && context.getApplicationContext() != null && keyReceiver != null) {
7266
context.getApplicationContext().unregisterReceiver(keyReceiver);
7367
}
7468
}

0 commit comments

Comments
 (0)