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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值