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; @Override
protected 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() {
@Override
public void onClick(View view) {
sendFirstNotification();
}
}); mSecondButton = (Button) findViewById(R.id.sendSecondNotificationButton);
mSecondButton.setOnClickListener(new OnClickListener() {
@Override
public 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 code
PendingIntent 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 code
PendingIntent 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"
> <Button
android:id="@+id/sendFirstNotificationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dip"
android:text="发送第一个通知" /> <Button
android: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. [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

    情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...

  2. pt-osc改表导致数据不一致案例分析

    2016-06-10 李丹 dba流浪猫 我们平时除了解决自己问题外,有时候也会协助圈内人士,进行一些故障排查,此案例就是帮某公司DBA进行的故障分析,因为比较典型,特分享一下,但仅仅是分享发生的过程 ...

  3. centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步

    目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务 ...

  4. sqoop中,如果数据中本身有换行符,会导致数据错位

    sqoop中,如果数据中本身有换行符,会导致数据错位: 解决办法: 在sqoop import时修改配置文件 sudo -u hive sqoop import --connect jdbc:mysq ...

  5. jmeter随笔(9)--有两种编码风格,导致数据乱码

    问题:在一个网站,有两种编码风格,导致数据乱码 解决办法: 1.首先设置jmeter的配置文件 2.针对要求是utf-8格式的这样的请求,做单独的编码处理(beanshell处理) 3.运行,在htm ...

  6. Flume简介与使用(二)——Thrift Source采集数据

    Flume简介与使用(二)——Thrift Source采集数据 继上一篇安装Flume后,本篇将介绍如何使用Thrift Source采集数据. Thrift是Google开发的用于跨语言RPC通信 ...

  7. 【Spark调优】大表join大表,少数key导致数据倾斜解决方案

    [使用场景] 两个RDD进行join的时候,如果数据量都比较大,那么此时可以sample看下两个RDD中的key分布情况.如果出现数据倾斜,是因为其中某一个RDD中的少数几个key的数据量过大,而另一 ...

  8. jQuery写省级联动列表,创造二维数组,以及如何存/调用二维数组中的数据

    jQuery写省级联动列表,创造二维数组来存放数据,然后通过each来遍历调用,通过creatTxtNode创建文本节点,通过createElement创建标签option,在通过append将文本写 ...

  9. Hibernate在PostgreSQL上执行sum函数导致数据失真的问题

    有一段通过Hibernate从PostgreSQL上进行sum统计的简单代码,但统计结果却导致数据失真,不知原因何在,求指教! Java代码片段如下: public List<Object> ...

随机推荐

  1. 使用 宝塔面板快速部署Java项目

    环境描述: 服务器系统:CentOS7 64位操作系统 面板版本:宝塔6.9.4 Nginx版本:Nginx 1.16 Tomcat版本:Tomcat7 JDK版本:1.8.0_121 环境部署就不用 ...

  2. 安装配置apache sentry服务

    环境 系统环境:Centos6.7 Hadoop版本:CDH5.10 jdk版本:jdk7 注:本文并未集成kerberos组件 安装Sentry Server 选择安装hive的节点进行安装测试: ...

  3. ZigBee学习三 UART通信

    ZigBee学习三 UART通信 在使用串口时,只需掌握ZigBee协议栈提供的串口操作相关的三个函数即可. uint8 HalUARTOpen(uint8 port,halUARTCfg_t *co ...

  4. home.php

    home.php <?php error_reporting(0); //抑制所有错误信息 @header("content-Type: text/html; charset=utf- ...

  5. GDKOI 游记

    Day 0 坐和谐号去广州 非常奇怪的一点是,每次坐车去广州人都很少,但是坐车回来人都贼多...... 到酒店住下,出去吃完晚饭,lmy开始奶:明天考数据结构啊! zkw:为什么不考AC自动机和插头D ...

  6. web标准,可用性和可访问性

    web标准,简单的说,是指html,css,JavaScript三者的分离. 网页由三部分组成:结构,表现和行为.对应的标准分为三方面: 1.结构化标准语言XHTML和XML2.表现标准语言主要包括c ...

  7. Repeater用ul li,一行显示多条数据

    原文发布时间为:2009-08-26 -- 来源于本人的百度文章 [由搬家工具导入] .rep {         width:680px;         float:left;         l ...

  8. 带接口的webservice 的发布

    1.接口:(打上service注解) package ws.qlq.a; import javax.jws.WebService; /** * 面向接口的webservice发布方式 * * */ @ ...

  9. PE笔记之PE基本结构图(PE笔记索引)

    PE(Portable Execute) 文件是Windows下可执行文件的总称,常见的有DLL,EXE,OCX,SYS等,事实上,一个文件是否是PE文件与其扩展名无关,PE文件可以是任 何扩展名.那 ...

  10. 小甲鱼PE详解之资源(PE详解11)

    原文出自:www.fishc.com 最近一直在安排第一届鱼C 学习班的事情,忙活了好一阵子,真是对不住大家,还大家久等了,这里要跟大家说声不好意思 ^_^ 今天我们来谈谈资源部分,资源部分可以说是 ...