安卓12广播
if(this.getPackageName() !=null){ intent.setPackage(this.getPackageName()); } F01AMainActivity.this.sendBroadcast(intent);
Intent intentCancel = new Intent(getApplicationContext(),NotificationBroadcastReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(getApplicationContext(),0,
intentCancel,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setDeleteIntent(pendingIntentCancel)//取消消息回调
/**
* ProjectName: Myditukaifa
* Author: lgq
* Date: 2018/3/12 0012 15:26
*/
public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int type = intent.getIntExtra("type", -1);
if (type != -1) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(type);
}
// if (action.equals("notification_cancelled")) {
//处理滑动清除和点击删除事件
Log.i("lgqq","body=====7777777777777777=NotificationBroadcastReceiver===="+action);
// }
ShareUtil.sharedPint("ifnotifica",0);
}
}
配置
<receiver android:name=".NotificationBroadcastReceiver">
<intent-filter>
<action android:name="notification_cancelled" />
</intent-filter>
</receiver>
方法2:
public class MessageNotificationService extends NotificationListenerService {
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.e("--------------", "--------------------------");//删除通知
}
@Override
public void onNotificationPosted(final StatusBarNotification sbn) {
final Notification mNotification = sbn.getNotification();
if (mNotification != null) {
Log.e("返回通知发起的时间=", sbn.getPostTime() + "");
Log.e("发送通知的包名=", sbn.getPackageName() + "");
Log.e("通知内容=", mNotification.tickerText + "");
}
}
}
<service
android:name=".test2.MessageNotificationService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService">
</action>
</intent-filter>
</service>
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);