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> ...
随机推荐
- Python之利用socketserver实现并发
socketserver这个模块是利用IO多路复用以及多线程实现并发的,可以让服务器同时建立多个链接 原理如图 我们这样更改服务器代码 import socketserver '''需要先写上一个类继 ...
- c++ 中的slipt实现
来自 http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html http://blog.diveinedu.com/%E4%B8%89%E7%A7%8D%E5 ...
- linux批量匹配移动文件的方法
需求 有需要移动的文件名清单 filename.txt filename.txt 内容如下: 15542842 1582457 1282427 1532158 4542457 1582453 6552 ...
- 微信公众平台OAuth2.0网页授权
微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使用这个的时候失败了或者无法理解其内容,希望我出个教程详细讲解一下,于是便有了这篇文章. 一. ...
- File IO(NIO.2):路径类 和 路径操作
路径类 Java SE 7版本中引入的Path类是java.nio.file包的主要入口点之一.如果您的应用程序使用文件I / O,您将需要了解此类的强大功能. 版本注意:如果您有使用java.io. ...
- 【转】UGUI EventSystem
EventSystem The EventSystem is a way of sending events to objects in the application based on inpu ...
- 【bzoj3879】SvT 后缀数组+倍增RMQ+单调栈
题目描述 (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置来表示), ...
- 被readLine()折腾了一把
虽然写IO方面的程序不多,但BufferedReader/BufferedInputStream倒是用过好几次的,原因是: 它有一个很特别的方法:readLine(),使用起来特别方便,每次读回来的都 ...
- linux之创建临时文件的方法
有时候,我们需要创建文件临时存放一些输出的信息,创建文件时就可能出现文件名存在的问题.如何创建唯一的文件名,Linux为我们提供几个方案: 1.mktemp(强烈推荐) The mktemp ut ...
- 当文字过长时裁剪(显示省略号或只裁剪 用CSS方法,不用程序)
原文发布时间为:2009-09-16 -- 来源于本人的百度文章 [由搬家工具导入] CSS中ellipsis()应用【转】 CSS手册中text-overflow属性的定义: 语法: text- ...