Android 通知删除事件监听,截获通知

 安卓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);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值