来不及解释,直接上效果图
抓去状态栏消息
Activity
NotificationMonitorService
AndroidManifest.xml
不想敲代码就复制下方代码
Androdi代码
自定义 NotificationMonitorService 继承 NotificationListenerService
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NotificationMonitorService extends NotificationListenerService {
// 在收到消息时触发
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// TODO Auto-generated method stub
Bundle extras = sbn.getNotification().extras;
// 获取接收消息APP的包名
String notificationPkg = sbn.getPackageName();
// 获取接收消息的抬头
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
// 获取接收消息的内容
String notificationText = extras.getString(Notification.EXTRA_TEXT);
Log.i("NotificationInfo", " Notification posted " + notificationTitle +" & " + notificationText);
}
// 在删除消息时触发
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// TODO Auto-generated method stub
Bundle extras = sbn.getNotification().extras;
// 获取接收消息APP的包名
String notificationPkg = sbn.getPackageName();
// 获取接收消息的抬头
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
// 获取接收消息的内容
String notificationText = extras.getString(Notification.EXTRA_TEXT);
Log.i("NotificationInfo", " Notification removed " + notificationTitle +" & " + notificationText);
}
}
注册 AndroidManifest.xml
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
Activity 代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 判断是否开启监听通知权限
if (NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName())) {
Intent serviceIntent =new Intent(this, NotificationMonitorService.class);
startService(serviceIntent);
}else {
// 去开启 监听通知权限
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
}
如果应用在后台被干掉 可以通过第三推送唤醒应用
本文介绍如何在Android中实现获取应用安装和第三方app推送通知。通过自定义`NotificationMonitorService`继承`NotificationListenerService`,并在`onNotificationPosted`和`onNotificationRemoved`方法中捕获和处理通知信息。在`AndroidManifest.xml`中注册服务,并在Activity中判断并开启监听权限。
4118

被折叠的 条评论
为什么被折叠?



