1. 创建service extends NotificationListenerService,并实现onNotificationRemoved、onNotificationPosted
public class NotificationCleanService extends NotificationListenerService {
private String TAG = "NotificationCleanService";
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.e(TAG, "通知被清除");
MessageHelper.getInstance().onNotificationRemoved(sbn);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.e(TAG, "有新通知");
Log.e(TAG, "该通知是否可以被清除:" + sbn.isClearable());
MessageHelper.getInstance().onNotificationPosted(sbn);
// StatusBarNotification[] sbns = getActiveNotifications(); // 返回当前系统所有通知的数组
// cancelAllNotifications(); // 删除系统中所有可被清除的通知
// cancelNotification(String key); // 删除具体某一个通知
// sbn.getId(); // 返回通知对应的id
// sbn.getNotification(); // 返回通知对象
// sbn.getPackageName(); // 返回通知对应的包名
// sbn.getPostTime(); // 返回通知发起的时间
// sbn.getTag(); // 返回通知的Tag,如果没有设置返回null
// sbn.isClearable(); // 返回该通知是否可被清楚,是否为FLAG_ONGOING_EVENT、FLAG_NO_CLEAR
// sbn.isOngoing(); // 返回该通知是否在正在运行的,是否为FLAG_ONGOING_EVENT
Notification notification = sbn.getNotification();
Bundle extras = notification.extras;
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
String content = extras.getString(Notification.EXTRA_TEXT, "");
Log.e(TAG, "通知标题:" + title + "\n 通知文本" + content);
if (content.contains("耗电") || title.contains("耗电")) {
// if (content.contains("USB") || title.contains("USB")) {
Log.e(TAG, "开始删除通知---通知包名:" + sbn.getPackageName() + "\n 通知Key" + sbn.getKey() + "\n 通知ID" + sbn.getId());
cancelNotification(sbn.getKey());
}
//打开自己的activity
try {
//隐式打开目标activity 目标activity androidmanifest需要配置
//注:配置category 为 DEFAULT 后,该activity不能作为应用启动activity
//
//
//
//
Intent intentNotification = new Intent("MyMainActivity");
//如果不需要隐式跳转可以直接用Class打开 该方式可以直接打开应用启动activity
// Intent intentNotification = new Intent(this, MainActivity.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);//由于不是activity的context,所以无法直接跳转
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentNotification, 0);
if (pendingIntent != null) {
pendingIntent.send();
}
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
//直接打开通知目标activity
if (content.contains("微信红包") || title.contains("微信红包")) {
//有红包,跳转到相应界面
PendingIntent pendingIntent = null;
pendingIntent = notification.contentIntent;
try {
if (pendingIntent != null) {
pendingIntent.send();
}
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
2.注册service
3.使用,判断是否有通知使用权,并启动service
if (!notificationListenerEnable()) {
gotoNotificationAccessSetting(MainActivity.this);
}
Intent intent = new Intent(MainActivity.this, NotificationCleanService.class);
startService(intent);
/**
* @return 跳转到通知使用权设置界面
*/
private boolean gotoNotificationAccessSetting(Context context) {
try {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity");
intent.setComponent(cn);
intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
context.startActivity(intent);
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}
/**
* @return 判断是否有通知使用权权限
*/
private boolean notificationListenerEnable() {
boolean enable = false;
String packageName = getPackageName();
String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
if (flat != null) {
enable = flat.contains(packageName);
}
return enable;
}
4.至此,onNotificationRemoved、onNotificationPosted 方法即可以接收到通知栏新到通知以及删除的通知,,收到通知时,可以拿取到通知相关信息做删除或跳转动作。红包助手,手环通知等功能就是用通知使用权做的。
5.现在只需要吧通知信息回调出来就行了,
public class MessageHelper {
private static MessageHelper messageHelper;
private MessageInterface messageInterface;
public static MessageHelper getInstance() {
if (messageHelper == null) {
messageHelper = new MessageHelper();
}
return messageHelper;
}
public void onNotificationRemoved(StatusBarNotification sbn) {
if (messageInterface != null) {
messageInterface.onNotificationRemoved(sbn);
}
}
public void onNotificationPosted(StatusBarNotification sbn) {
if (messageInterface != null) {
messageInterface.onNotificationPosted(sbn);
}
}
/**
* 设置回调方法
*
* @param messageInterface 通知监听
*/
public void setNotifyListener(MessageInterface messageInterface) {
this.messageInterface = messageInterface;
}
public interface MessageInterface {
void onNotificationRemoved(StatusBarNotification sbn);
void onNotificationPosted(StatusBarNotification sbn);
}
}
//消息监听
MessageHelper.getInstance().setNotifyListener(new MessageHelper.MessageInterface() {
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Notification notification = sbn.getNotification();
Bundle extras = notification.extras;
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
String content = extras.getString(Notification.EXTRA_TEXT, "");
mTv.setText(mTv.getText() + "\n 通知被清除:标题:" + title + " 内容:" + content);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification notification = sbn.getNotification();
Bundle extras = notification.extras;
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
String content = extras.getString(Notification.EXTRA_TEXT, "");
mTv.setText(mTv.getText() + "\n 有新通知:标题:" + title + " 内容:" + content + " 包名:" + sbn.getPackageName() + " 清除:" + sbn.isClearable());
}
});