android状态栏中多个通知冲突的问题

本文介绍了解决Android应用中通知Intent重复的问题。通过使用不同的requestCode值确保每个通知指向正确的Intent,避免了多个通知指向同一Intent的情况。同时提供了一段关于下载完成后的通知栏提示通知的完整示例代码。

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

网上看到的解决办法:

如果用相同的通知id, 该怎么告诉处理通知的活动,每个通知的内容呢?
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
最后一个参数可以保证显示的是最新的那个通知
如果用不同的通知id, 为什么处理通知的活动得到的Intent总是第一个通知的Intent呢?
多个Intent是根据它们的Data属性来区分的,如果Data相同,将认为是同一个Intent 

 

实践之后不是很好用,请教别人找到了解决办法。

PendingIntent .getActivity(Context  context, int requestCode,Intent  intent, int flags)

requestCode 值如果一样,就会出现多个通知都指向一个intent。

只要requestCode不一样就可以解决问题了!

 

 

贴一段完整的代码:下载完成后通知栏出现提示通知

 java代码:

 

[java] view plain copy
  1. /**  
  2.    * 状态栏消息通知  下载完成  
  3.    * @param context  
  4.    * @param name  
  5.    */   
  6.   public   static   void  notifyTaskFinishToStatusBar(Context context,String name)  
  7.      {  
  8.    String text;  
  9.   if (name!= null ){  
  10.    text=name+context.getString(R.string.has_download);  
  11.   }else {  
  12.    text=context.getString(R.string.has_download);  
  13.   }     
  14.      
  15.   NotificationManager nfm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
  16.         Notification notification = new  Notification(R.drawable.icon, text,System.currentTimeMillis());  
  17.         notification.flags=Notification.FLAG_AUTO_CANCEL;//点击自动清除通知   
  18.         Intent openintent = new  Intent();  
  19.         openintent.setClass(context, MainActivity.class );  
  20.         Bundle data=new  Bundle();  
  21.         data.putInt(Constants.BOOT_INDEX_TAG, MainActivity.downloadPageLoadedState);  
  22.         openintent.putExtras(data);  
  23.           
  24.   PendingIntent contentIntent = PendingIntent.getActivity(context, 1 , openintent,  0 );  
  25.     
  26.         notification.setLatestEventInfo(context, context.getString(R.string.qc_download_tip),text, contentIntent);  
  27.         nfm.notify(Constants.QC_DOWNLOAD_NOTIFY, notification);  
  28.      } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值