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>
时间: 2024-08-09 01:30:53

Notification(二)——PendingIntent的flag导致数据同样的问题的相关文章

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.NotificationMa

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

2016-06-10 李丹 dba流浪猫 我们平时除了解决自己问题外,有时候也会协助圈内人士,进行一些故障排查,此案例就是帮某公司DBA进行的故障分析,因为比较典型,特分享一下,但仅仅是分享发生的过程,不对该案例的产生以及如何避免做过多评价! pt-online-schema-change:是对大表进行在线alter操作,并尽量避免影响线上业务,这是最优秀的mysql管理工作之一,在平时的工作中,帮助我们胜多. 环境说明 pt-osc 版本:percona-toolkit-2.2.14 mysq

[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

情况是这样的,使用NotificationManager触发多个Notification: Java代码   private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent){ Notification notification = new Notification(icon, tickerText, S

跟我上“云”端(二)多租户的数据隔离

多租户两种极端实现方式 启用多租户的方法有很多,我不想做一一的介绍,但是有两个极端我们可以考虑一下: 极端一: 是所有租户共享单一应用程序实例,也就是相同的服务器.中间件和应用程序.实现的方法是用租户标识参数对应用程序的单一实例进行参数化. 图1 在多个租户之间共享应用程序和中间件的单一实例 极端二: 是租户在单独的服务器上运行自己的应用程序实例(当前许多 Application Service Provider [ASP] 采用这种方法). 租户只共享数据中心的基础结构(比如供电和制冷),但是

你知道怎么解决DB读写分离,导致数据不一致问题吗?

目录 前言 为什么产生数据不一致 方案一:利用数据库自身特性 方案二:不解决 方案三:客户端保存法 方案四:缓存标记法 方案五:本地缓存标记 前言 在互联网中大型项目中,读写分离应该是我们小伙伴经常听说的,这个主要解决大流量请求时,提高系统的吞吐量.因为绝大部分互联网产品都是读多写少,大部分都是读请求,很小部分是写请求. 上图: 1.一个主库负责写请求,更新数据2.两个从库负责读请求,可以提高系统吞吐量3.主库和从库之间同步数据 为什么产生数据不一致 上图中业务流程 1.写请求A进行数据更新,但

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

目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务安装 2.3 Rsync命令详解 2.3 实验一使用rsync命令备份数据 2.4 实验二使用rsyncd服务进行数据备份 3.Rsync+sersync实现数据实时同步 3.1 rsync+sersync架构作用 3.2 rsync+inotify-tools与rsync+sersync架构的区别

层次结构和二维表的关系数据存储

摘:Storing Hierarchical Data in a Database Article(翻译版本) 原文链接:http://shiningray.cn/hierarchical-data-database.html 作者:Gijs Van Tulder 翻译:ShiningRay @ NirvanaStudio 无论你要构建自己的论坛,在你的网站上发布消息还是书写自己的CMS程序,你都会遇到要在数据库中存储层次数据的情况.同时,除非你使用一种像XML的数据库,否则关系数据库中的表都不

如何增大二维码种的数据存储

最近在做项目,需求就是要传尽量多的数据,属于一个调研,估计后期会加入到项目里面,二维码其实不推荐放入大量数据,但是需求嘛,拿钱干活,可能后期还会优化,因为目前的画扫描的效率不高,主要直接用的原生的开源库zxing感觉效率不高. 一开始,从数据源下手,进行数据源压缩,首先尝试了hufman编码,huffman主要是一些开源的算法,但是实现从建树到压缩,其实有很大区别,文件我直接上传了,有兴趣的可以看一下: http://download.csdn.net/detail/shidongdong201

Hive语法层面优化之五分析执行计划追踪导致数据倾斜的原因

count(distinct key)案例 explain select count(distinct session_id) from trackinfo where ds=' 2013-07-21' ; STAGE DEPENDENCIES: Stage-1 is a root stage Stage-0 is a root stage STAGE PLANS: Stage: Stage-1 Map Reduce Alias -> Map Operator Tree: trackinfo T