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

来源:互联网 发布:handler源码解析 编辑:程序博客网 时间:2024/05/07 04:15
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;@Overrideprotected 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() {@Overridepublic void onClick(View view) {sendFirstNotification();}});mSecondButton = (Button) findViewById(R.id.sendSecondNotificationButton);mSecondButton.setOnClickListener(new OnClickListener() {@Overridepublic 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 codePendingIntent 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", "来自第一个按钮触发的通知",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 codePendingIntent 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", "来自第二个按钮触发的通知",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>


0 0
原创粉丝点击