MainActivity例如以下:

package cc.cu;import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
/*** Demo描写叙述:* 两个Notification均使用Intent携带数据时.当收到第一个通知时取出其携带的数据没有问题,数据准确;* 可是当收到第二个通知时取出其携带的数据时,竟然发现是第一个通知携带的数据.* 当时出现这个问题时,第一感觉问题在于* NotificationManager.notify(int id, Notification notification)* 方法里的id值同样造成的.但将其改动为不同的值后发现问题依然.* * 后来发现问题出现于方法:* PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)* 的最后一个參数.该值共同拥有四个常量.最好是使用PendingIntent.FLAG_UPDATE_CURRENT,该值的解释例如以下:* * Flag indicating that if the described PendingIntent already exists, * then keep it but replace its extra data with what is in this new Intent* * 假设PendingIntent已经存在,那么保留它而且仅仅替换它的extra数据* * 參考资料:* 1 http://blog.csdn.net/lilu_leo/article/details/8491738* 2 http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT* 3 http://blog.csdn.net/vipzjyno1/article/details/25248021*   Thank you very much*   在资料3中对于Notification作了非常全面和具体的介绍.有兴趣的能够看看.* * 备注说明:* 測试环境Android2.3.6**/
public class MainActivity extends Activity {private Context mContext;private Button mFirstButton;private Button mSecondButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();// 取出通知携带的数据if (this.getIntent().getExtras() != null) {String data = this.getIntent().getExtras().getString("testData");System.out.println("得到通知传过来的数据:" + data);}}private void init() {mContext = this;mFirstButton = (Button) findViewById(R.id.sendFirstNotificationButton);mFirstButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {sendFirstNotification();}});mSecondButton = (Button) findViewById(R.id.sendSecondNotificationButton);mSecondButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {sendSecondNotification();}});}// 发送通知private void sendFirstNotification() {Notification notification = new Notification();Intent intent = new Intent(mContext, MainActivity.class);intent.putExtra("testData", "来自first的数据");// PendingIntent pendingIntent=PendingIntent.getActivity(mContext, 0,intent, 0);//error codePendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.icon = R.drawable.ic_launcher;notification.defaults = Notification.DEFAULT_SOUND;notification.flags |= Notification.FLAG_AUTO_CANCEL;notification.tickerText = "第一个通知";notification.setLatestEventInfo(mContext, "通知1", "来自第一个button触发的通知",pendingIntent);NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.notify(0, notification);}// 发送通知private void sendSecondNotification() {Notification notification = new Notification();Intent intent = new Intent(mContext, MainActivity.class);intent.putExtra("testData", "来自second的数据");// PendingIntent pendingIntent=PendingIntent.getActivity(mContext, 0,intent, 0);//error codePendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.icon = R.drawable.ic_launcher;notification.defaults = Notification.DEFAULT_SOUND;notification.flags |= Notification.FLAG_AUTO_CANCEL;notification.tickerText = "第二个通知";notification.setLatestEventInfo(mContext, "通知2", "来自第二个button触发的通知",pendingIntent);NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.notify(1, notification);}}

main.xml例如以下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/sendFirstNotificationButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="100dip"android:text="发送第一个通知" /><Buttonandroid:id="@+id/sendSecondNotificationButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="200dip"android:text="发送第二个通知" /></RelativeLayout>

Notification(二)——PendingIntent的flag导致数据同样的问题相关推荐

  1. SAP LSMW 因物料描述里有换行符或者引号导致数据导入失败

    SAP LSMW 因物料描述里有换行符或者引号导致数据导入失败 近期,笔者所在的D项目进入第一轮主数据导入的阶段.导入物料主数据由笔者负责. 我的LSMW工具如下方式: 采用的是standard ba ...

  2. [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

    情况是这样的,使用NotificationManager触发多个Notification: Java代码   private Notification genreNotification(Contex ...

  3. android 一维数组遍历,$.each()循环遍历一维数组、二维数组、JSON数据和DOM元素

    本文介绍$.each()循环遍历一维数组.二维数组.JSON数据和DOM元素. 请注意在使用JQuery $.each()方法前,要首先引用jquery库文件. $.each()循环遍历一维数组 Jq ...

  4. IE浏览器下ajax缓存导致数据不更新的解决方法

    IE浏览器下ajax缓存导致数据不更新的解决方法 参考文章: (1)IE浏览器下ajax缓存导致数据不更新的解决方法 (2)https://www.cnblogs.com/xcsn/p/4745437 ...

  5. iOS 数据计算带小数点导致数据不精确问题

    iOS 数据计算带小数点导致数据不精确问题 我在开发中碰到一个UIScrollView跳动的问题,明明各个地方都没有问题,最后发现原来是计算ContentSize时有个计算需要除以2 /2.00 我这 ...

  6. 文件系统损坏导致数据文件异常恢复----惜分飞

    今天接到一个客户的服务请求,由于服务器被强制重启,数据库无法启动 ORA-1200报错 这是一个常见的ORA-1200错误,但是文件大小相差的有离谱实际大小729600个block,但是现在只有149 ...

  7. matlab生成二维服从高斯分布的数据

    matlab生成二维服从高斯分布的数据 2015-12-30 21:31 1263人阅读 评论(0) 收藏 举报  分类: matlab(8)  由于实验需要,需要生成两类模式的数据,同时这两类数据要 ...

  8. 页面缓存导致数据错误

    页面缓存导致数据错误 服务端渲染(ssr)会将 API 数据做页面元素返回给前端, 而浏览器会认为是静态页面并将页面缓存, 导致每次访问时都是缓存用户数据. 页面又包含对数据的处理并提交. 当多端更新 ...

  9. php二维数组json,php如何将二维数组转为json数据

    php将二维数组转为json数据的方法:可以利用php内置函数json_encode()来实现.json_encode()函数用于对变量进行json编码,如果执行成功则返回json数据,否则返回fal ...

最新文章

  1. 【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )
  2. 板材开料最优算法_板材套料软件AutoNest与Tekla Structures协同工作实现设计到加工自动化...
  3. 合并报表调整、抵销分录的编制顺序
  4. mysql终端_MySQL之终端(Terminal)管理MySQL
  5. Hive的内置数据类型
  6. 年后跳槽必备的 Java 题库,全网最全!
  7. 还在使用OpenGL ES做渲染,你Out了,赶紧来拥抱Vulkan吧~
  8. 2020-03-13 MySQL 8 绿色安装
  9. 深入了解gorm Scan的使用
  10. Android 脚本设计之 SL4A
  11. linux安装python教程视频_新手开发者的极简Linux上手Python视频教程
  12. 串口控制计算机重启,电脑开机无限重启解决方法
  13. [轻松学会shell编程]-3、grep、正则表达式、awk的详细用法、分析系统自带的两个文件(functions和network)
  14. 百度搜索结果页面的参数_反馈搜索结果用时(rsv_sug4)
  15. magento 模块化开发_Magento中的PayPal信用卡令牌化
  16. error lnk1123:转换到coff期间失败
  17. MySQL数据库编程--函数
  18. Biu~送你 20 个提供远程工作的网站,都很棒
  19. 基于 Ceph 的 Kubernetes 数据持久化
  20. 全自动安装的XP补丁包

热门文章

  1. MyBatisPLus3.x中代码生成器自定义数据库表字段类型转换
  2. Webservice开发之xsd中开发list请求参数的接口
  3. 使用Jquery获取thymeleaf中checkbox的值
  4. Tomcat下项目调整Log4J的console输出级别,减少输出信息
  5. Python中使用turtle绘制多个同心圆
  6. 交互式计算机图形学总结:第七章 离散技术
  7. goj基础环境的配置
  8. 神策数据张涛:企业服务客户全生命周期运营三步曲:执行反馈
  9. 使用python3 解析html对称标签
  10. 探索MySql.Data.dll