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面向对象之什么是类(1)
1.C#.Java :只能用面向对象编程 Ruby.Python :函数编程+ 面向对象 面向对象编程不是在所有地方都比函数式编程方便的,类是为了封装,下面是简单的使用方法 在创建类的时候要用clas ...
- Solr安装 win系统
安装之前需查看:https://lucene.apache.org/solr/guide/7_6/solr-system-requirements.html#solr-system-requireme ...
- nginx的进程模型
nginx采用的也是大部分http服务器的做法,就是master,worker模型,一个master进程管理站个或者多个worker进程,基本的事件处理都是放在woker中,master负责一些全局初 ...
- [Cocos2dx Bug] [win32] Function CCFileUtils::fullPathFromRelativeFile forget consider the path separated by '\\'
[Cocos2dx 2.2.4] [win32平台Bug] const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilen ...
- this.$router.push() 在新窗口怎么打开
参考:https://blog.csdn.net/qyl_0316/article/details/86550361
- Intelli IDEA快捷键(配合IdeaVim)
Intelli IDEA开发环境,个人总结的一些常用的快捷键. 想要使用vim方式编辑代码,可以使用Intelli IDEA的IdeaVim.IdeaVim插件功能很强大,在vim编辑模式下还可以使用 ...
- Master of Sequence
Master of Sequence 时间限制: 10 Sec 内存限制: 128 MB 题目描述 There are two sequences a1,a2,...,an , b1,b2,..., ...
- Bzoj1974 [Sdoi2010]auction 代码拍卖会
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 375 Solved: 151 Description 随着iPig在P++语言上的造诣日益提升,他形成 ...
- SQL Server 2005 无法连接到 127.0.0.1
[问题描述]如果连接本机的数据库服务器,服务器名称可填: 127.0.0.1或localhost或本机机器全名(比如HOME),有时候用本机机器名可以登录SQL Server 2005,但用127.0 ...
- 常见的 Git 命令:
开始一个工作区(参见:git help tutorial) clone 克隆一个仓库到一个新目录 init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库 在当前变更上工作(参见:git he ...