监听Android通知栏消息适配

Posted by JamesPxy on 2018-09-29

1.关于vivo手机真机调试,安装失败

在gradle.properties,添加

android.injected.testOnly = false

2.OPPO短信监听的action有改变

	 <!--oppo 手机的短信action-->
     <action android:name="android.provider.OppoSpeechAssist.SMS_RECEIVED" />
     <action android:name="android.provider.Telephony.SMS_RECEIVED" />

3.监听Android通知栏消息

private String QQ_PACKAGE = "com.tencent.mobileqq|com.tencent.qqlite";
private String WX_PACKAGE = "com.tencent.mm";
private String CALL_PACKAGE = "com.android.incallui";
private String SMS_PACKAGE = "com.android.mms|com.android.mms.service";

继承NotificationListenerService在onNotificationPosted()回调获取消息

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

private String QQ_PACKAGE = "com.tencent.mobileqq|com.tencent.qqlite";
private String WX_PACKAGE = "com.tencent.mm";
private String CALL_PACKAGE = "com.android.incallui";
private String SMS_PACKAGE = "com.android.mms|com.android.mms.service";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtil.e("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
LogUtil.e("单参数 onNotificationPosted ID :" + sbn.getNotification().tickerText);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
super.onNotificationPosted(sbn, rankingMap);
CharSequence cs = sbn.getNotification().tickerText;
String pk = sbn.getPackageName();
if (null == pk || null == cs) {
return;
}
LogUtil.d("多参数 收到数据: onNotificationPosted rankingMap ID :"
+ sbn.getId() + "\t"
+ sbn.getNotification().tickerText + "\t"
+ sbn.getPackageName());
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
Log.d("AAA", "多参数 onNotificationPosted ID :"
+ sbn.getId() + "\t"
+ sbn.getNotification().tickerText + "\t"
+ sbn.getPackageName());
}

@Override
public void onDestroy() {
LogUtil.d("onDestroy");
startService(new Intent(this, NotifyService.class));
super.onDestroy();
}
}

别忘了在manifest中注册:

1
2
3
4
5
6
7
<service
android:name="com.lvshou.hxs.service.NotifyService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>


支付宝打赏 微信打赏

赞赏一下