Android 监听Notification 被清除实例代码

本文介绍如何在Android中管理和操作通知,包括使用BroadcastReceiver监听通知删除事件、初始化通知、发送直播通知等,并强调了setDeleteIntent的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class NotifiDoWhatReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || context == null) {
            return;
        }
        String action = intent.getAction();
        if(action.equals(NOTIFICATION_DELETED_ACTION)){
            int type = intent.getIntExtra(PUSH_TYPE,-1);
            int notiid  = intent.getIntExtra(PUSH_ID,-1);
            String url = intent.getStringExtra(Constant.DATA);
            if(type==0){
                //apk下载
                ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notiid);
                if(downCalls.containsKey(url)){
                    downCalls.remove(url);
                }
            }
        }
    }

}

 public void initNotification() {
        builder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("0%")
                .setContentTitle("初心客厅")
                .setProgress(100, 0, false).setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_APK,
		 new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_ID, NOTIFICATION_ID_APK).putExtra(PUSH_TYPE, 0).putExtra(Constant.DATA, url), 0));
        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID_APK, builder.build());
    }


参考资料http://www.jb51.net/article/88974.htm

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
 
 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent == null || context == null) {
   return;
  }
 
  mNotificationManager.cancel(NOTIFICATION_ID_LIVE);
 
  String type = intent.getStringExtra(PUSH_TYPE);
  if (PUSH_TYPE_LINK.equals(type)) {
   //mNumLinkes = 0;
  } else if (PUSH_TYPE_LIVE.equals(type)) {
   //mNumLives = 0;
  }
  //这里可以重新计数
 }
};
 
 
private void sendLiveNotification() {
 Intent intent = new Intent(NOTIFICATION_CLICK_ACTION);
 
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
 
 String title = "Push测试";
 mBuilder.setContentTitle(title);
 mBuilder.setTicker(title);
 mBuilder.setContentText("https://233.tv/over140");
 mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
 mBuilder.setSmallIcon(R.drawable.ic_action_cast);
 mBuilder.setDefaults(Notification.DEFAULT_ALL);
 mBuilder.setWhen(System.currentTimeMillis());
 mBuilder.setContentIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, intent, 0));
 mBuilder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0));
 
 mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build());
}

代码说明

  1、最重要的是setDeleteIntent,这个在API Level 11(Android 3.0) 新增的

  2、注意不要设置setAutoCancel为true,否则监听器接收不到

  3、这里统一了点击通知和消除通知的操作

  4、注意广播在推送前要注册好

实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听。

以上就是对Android Notification 事件的资料整理,希望能帮助Android开发的朋友。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值