Notification(二)——PendingIntent的flag导致数据同样的问题
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导致数据同样的问题的更多相关文章
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...
- pt-osc改表导致数据不一致案例分析
2016-06-10 李丹 dba流浪猫 我们平时除了解决自己问题外,有时候也会协助圈内人士,进行一些故障排查,此案例就是帮某公司DBA进行的故障分析,因为比较典型,特分享一下,但仅仅是分享发生的过程 ...
- centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步
目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务 ...
- sqoop中,如果数据中本身有换行符,会导致数据错位
sqoop中,如果数据中本身有换行符,会导致数据错位: 解决办法: 在sqoop import时修改配置文件 sudo -u hive sqoop import --connect jdbc:mysq ...
- jmeter随笔(9)--有两种编码风格,导致数据乱码
问题:在一个网站,有两种编码风格,导致数据乱码 解决办法: 1.首先设置jmeter的配置文件 2.针对要求是utf-8格式的这样的请求,做单独的编码处理(beanshell处理) 3.运行,在htm ...
- Flume简介与使用(二)——Thrift Source采集数据
Flume简介与使用(二)——Thrift Source采集数据 继上一篇安装Flume后,本篇将介绍如何使用Thrift Source采集数据. Thrift是Google开发的用于跨语言RPC通信 ...
- 【Spark调优】大表join大表,少数key导致数据倾斜解决方案
[使用场景] 两个RDD进行join的时候,如果数据量都比较大,那么此时可以sample看下两个RDD中的key分布情况.如果出现数据倾斜,是因为其中某一个RDD中的少数几个key的数据量过大,而另一 ...
- jQuery写省级联动列表,创造二维数组,以及如何存/调用二维数组中的数据
jQuery写省级联动列表,创造二维数组来存放数据,然后通过each来遍历调用,通过creatTxtNode创建文本节点,通过createElement创建标签option,在通过append将文本写 ...
- Hibernate在PostgreSQL上执行sum函数导致数据失真的问题
有一段通过Hibernate从PostgreSQL上进行sum统计的简单代码,但统计结果却导致数据失真,不知原因何在,求指教! Java代码片段如下: public List<Object> ...
随机推荐
- (Mac)centos 6.5安装 JDK+mysql
为了把自己的网站放到外网,购买了阿里云的centos 6.5服务器,以下是安装 JDK 一.JDK安装: 方法一: 1.创建目录,命令行:(这里可以不加sudo) sudo mkdir /jdk 2. ...
- 逆向中静态分析工具——IDA初学者笔记
逆向中静态分析工具——IDA初学者笔记 //****************************************************************************** ...
- BZOJ4872 [六省联考2017]分手是祝愿 【期望dp】
题目 Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为 从 1 ...
- git 以及 工作区 版本库 暂存区
https://www.jianshu.com/p/a308acded2ce 这个博客介绍的比较简单 https://blog.csdn.net/qq_31828515/arti ...
- 记Django数据库迁移过程中遇到的一些问题
首先描述一下问题,Django 数据库使用的mysql, 然后开始没注意,没建一个default库,就把第一个数据库当成默认的了,结果Django的admin相关的那些表,都自动生成到这个库里了,现在 ...
- SQL语句的执行顺序(转载+不同意见)
MySQL的语句一共分为11步,如下图所标注的那样,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来 ...
- [转]谈谈Java中的语法糖
*该博客转自 http://blog.csdn.net/danchu/article/details/54986442 语法糖(Syntactic Sugar),也称糖衣语法,指在计算机语言中添加的某 ...
- hdu 3061 hdu 3996 最大权闭合图 最后一斩
hdu 3061 Battle :一看就是明显的最大权闭合图了,水提......SB题也不说边数多少....因为开始时候数组开小了,WA....后来一气之下,开到100W,A了.. hdu3996. ...
- LeetCode OJ-- Flatten Binary Tree to Linked List **
https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地. ...
- 联通积分兑换的Q币怎么兑换到QQ上
可登录联通积分商城http://jf.10010.com 查询和兑换Q币, 1,通过联通积分商城自主兑换,提交订单扣除积分成功后,10010端口将自动为您下发验证码短信. 2,在有效期内登陆Q币充值 ...