Notification——PendingIntent的flag导致数据同样的问题

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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导致数据同样的问题的主要内容,如果未能解决你的问题,请参考以下文章

navigator.notification.* 失败“navigator.notification [undefined] 不是对象”

Notification通知栏

Notification通知栏

Notification(Notification的通知栏常驻各种样式点击无效禁止滑动删除兼容低版本)

求教notification问题

Notification