1. 程式人生 > >Notification(二)——PendingIntent的flag導致數據同樣的問題

Notification(二)——PendingIntent的flag導致數據同樣的問題

text package sts leo class put evel listen 觸發

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導致數據同樣的問題