https://cloud.tencent.com/developer/article/1876168

创建一个服务,然后将服务注册在AndroidManifest.xml中。创建一个NotifyService类,里面的代码如下:

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
public class NotifyService extends NotificationListenerService {

public static final String TAG = "NotifyService";

public static final String QQ = "com.tencent.mobileqq";//qq信息
public static final String WX = "com.tencent.mm";//微信信息
public static final String MMS = "com.android.mms";//短信
public static final String HONOR_MMS= "com.hihonor.mms";//荣耀短信
public static final String MESSAGES = "com.google.android.apps.messaging";//信息
public static final String IN_CALL = "com.android.incallui";//来电 -
/**
* 发布通知
* @param sbn 状态栏通知
*/
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
//消息内容
String msgContent = "";
if (sbn.getNotification().tickerText != null) {
String msgContent = sbn.getNotification().tickerText.toString();
}
switch (sbn.getPackageName()){
case MESSAGES:
case MMS:
case HONOR_MMS:
Log.d(TAG,"收到短信");
break;
case QQ:
Log.d(TAG,"收到QQ消息");
break;
case WX:
Log.d(TAG,"收到微信消息");
break;
case IN_CALL:
Log.d(TAG,"收到来电");
break;
default:break;
}
}

/**
* 通知已删除
* @param sbn 状态栏通知
*/
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
switch (sbn.getPackageName()){
case MESSAGES:
case MMS:
case HONOR_MMS:
Log.d(TAG,"移除短信");
break;
case QQ:
Log.d(TAG,"移除QQ消息");
break;
case WX:
Log.d(TAG,"移除微信消息");
break;
case IN_CALL:
Log.d(TAG,"移除来电");
break;
default:break;
}
}

/**
* 监听断开
*/
@Override
public void onListenerDisconnected() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 通知侦听器断开连接 - 请求重新绑定
requestRebind(new ComponentName(this, NotificationListenerService.class));
}
}
}

下面将这个服务注册在AndroidManifest.xml中。

1
2
3
4
5
6
7
8
9
10
        <!--通知监听服务-->
<service
android:name=".NotifyService"
android:enabled="true"
android:label="测试通知服务"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>

打开通知服务监听

使用这个通知服务其实就是打开一个手机上应用的开关,效果上和打开蓝牙差不多,下面先写一个方法检查当前应用是否开启这个服务。方法代码如下:

1
2
3
4
5
6
7
8
9
10
11
/**
* 是否启用通知监听服务
* @return
*/
public boolean isNLServiceEnabled() {
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(this);
if (packageNames.contains(getPackageName())) {
return true;
}
return false;
}

这里还对应一个方法就是设置服务是否运行,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 切换通知监听器服务
*
* @param enable
*/
public void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), NotifyService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), NotifyService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

现在方法有了需要一个地方去触发,通过按钮来进行,在activity_main.xml添加一个按钮。

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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请求权限"
android:onClick="requestPermission"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在MainActivity中添加一个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static final int REQUEST_CODE = 9527;
/**
* 请求权限
*
* @param view
*/
public void requestPermission(View view) {
if (!isNLServiceEnabled()) {
startActivityForResult(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"), REQUEST_CODE);
} else {
showMsg("通知服务已开启");
toggleNotificationListenerService(true);
}
}

这个还有一个showMsg方法:

1
2
3
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

然后是页面返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (isNLServiceEnabled()) {
showMsg("通知服务已开启");
toggleNotificationListenerService(true);
} else {
showMsg("通知服务未开启");
toggleNotificationListenerService(false);
}
}
}