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> ...
随机推荐
- THUSC2018 爆零记
没想到我还真能过这个...... 太玄学了= = 不过这直接导致我月考数学挂科,掉出年级前十= = 5.26 THU过了! 真是十分意外的惊喜啊$-\omega-$ 6.1 今天出发去帝都! 然而飞行 ...
- pat Public Bike Management (30)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- Codeforces Round #315 (Div. 2) B 水题强行set
B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- MFC 对话框阴影效果
在 OnInitDialog(Cdialog)里面添加 SetClassLong(this->m_hWnd, GCL_STYLE, GetClassLong(this->m_hWnd, G ...
- Apache Commons 工具集介绍
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...
- python没学好
环境 装东西用pip3不是pip 运算 //是下取整 py的浮点数也没有大小限制,但是超出一定范围就直接表示为inf 条件语句与循环 if xxx:, elif:, else:, 'for x in ...
- DataSet导出到Excel文件
public static void ExportToExcel(DataSet source, string fileName) { System.IO.StreamWriter excelDoc ...
- [LeetCode] Search Insert Position 二分搜索
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- autofac 用法总结
autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...
- 003为什么shell的配置文件那么繁琐?
Bash的设置之所以如此繁琐,是由于历史原因造成的.早期的时候,计算机运行速度很慢,载入配置文件需要很长时间,Bash的作者只好把配置文件分成了几个部分,阶段性载入. 系统的通用设置放在 /etc/p ...