Notification中多个Intent使用putExtra方法携带数据,在目的activity中通过getIntent方法取出传递数据时,有时候会发现取出来的数据都是第一次putExtra中放入的数据。

下面看下代码,比较简单:

package com.example.notificationdemo;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FirstActivity extends Activity {private Button mButton;private NotificationManager mNotificationManager;private Intent mIntent;private PendingIntent mPendingIntent;Notification mNotification;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first);mNotification = new Notification();mIntent = new Intent(FirstActivity.this, MainActivity.class);mIntent.putExtra("hello", "first");Log.v("@@@@@@", "this is put in intent first....");mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);//主要在于PendingIntent的getActivity方法中的参数mPendingIntent = PendingIntent.getActivity(FirstActivity.this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);//如果使用下面注释掉的代码,将会出现上面说讲到的问题,当然在SecondActivity中也必须做修改//mPendingIntent = PendingIntent.getActivity(FirstActivity.this, 0, mIntent, 0);mButton = (Button)findViewById(R.id.first_button);mButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {showNotification();}});}private void showNotification() {  mNotification.icon = R.drawable.message;mNotification.defaults = Notification.DEFAULT_SOUND; mNotification.flags |= Notification.FLAG_AUTO_CANCEL;mNotification.tickerText = "第一个";mNotification.setLatestEventInfo(FirstActivity.this, "第一个","第一个", mPendingIntent);mNotificationManager.notify(0, mNotification);}}

第二个Activity:

package com.example.notificationdemo;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SecondActivity extends Activity {private Button mButton;private NotificationManager mNotificationManager2;private Intent mIntent;private PendingIntent mPendingIntent2;Notification mNotification2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);mNotification2 = new Notification();mIntent = new Intent(SecondActivity.this, MainActivity.class);mIntent.putExtra("hello", "second");Log.v("@@@@@@", "this is doing in second activity");mNotificationManager2 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 1, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);//如果使用下面注释掉的代码,将会出现上面说讲到的问题,当然在FirstActivity中也必须做修改(把上面代码注释掉,使用下面行代码,就会重现)//mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 0, mIntent, 0);mButton = (Button) findViewById(R.id.second_button);mButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {showNotification();}});}private void showNotification() {    mNotification2.icon = R.drawable.message2;mNotification2.defaults = Notification.DEFAULT_SOUND; mNotification2.flags |= Notification.FLAG_AUTO_CANCEL;mNotification2.tickerText = "第二个";Log.v("@@@@@@", "intent put second ...");mNotification2.setLatestEventInfo(SecondActivity.this, "第二个", "第二个", mPendingIntent2);Log.v("@@@@@@", "do shownotification in second activity");mNotificationManager2.notify(0, mNotification2);}}

在MainActivity中接受两个Activity中的Notification传过来的数据:

package com.example.notificationdemo;import android.os.Bundle;import android.app.TabActivity;import android.content.Intent;import android.graphics.Color;import android.util.Log;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TabHost;public class MainActivity extends TabActivity implements OnCheckedChangeListener{private TabHost mTabHost;private RadioGroup mRadioGroup;private RadioButton mFirstRadio, mSecondRadio;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mTabHost = getTabHost();mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this, FirstActivity.class)));mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this, SecondActivity.class)));                mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);mRadioGroup.setOnCheckedChangeListener(this);mFirstRadio = (RadioButton)findViewById(R.id.first_radio);mSecondRadio = (RadioButton) findViewById(R.id.second_radio);Log.v("@@@@@@", "this is doing  in onCreate");        ChangeChecked(true);        String str = getIntent().getStringExtra("hello");    Log.v("@@@@@@", "the str is " + str);    if (str != null && str.equals("first")) {    ChangeChecked(true);    mTabHost.setCurrentTabByTag("first");    } else if (str != null && str.equals("second")) {    ChangeChecked(false);    mTabHost.setCurrentTabByTag("second");    }            }        @Override    protected void onNewIntent(Intent intent) {        String str = intent.getStringExtra("hello");    if (str != null && str.equals("first")) {    ChangeChecked(true);    mTabHost.setCurrentTabByTag("first");    } else if (str != null && str.equals("second")) {    ChangeChecked(false);    mTabHost.setCurrentTabByTag("second");    }    super.onNewIntent(intent);        }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.first_radio:mTabHost.setCurrentTabByTag("first");ChangeChecked(true);break;case R.id.second_radio:mTabHost.setCurrentTabByTag("second");ChangeChecked(false);break;default :break;}}private void ChangeChecked(boolean bool) {if (bool) {mFirstRadio.setTextColor(Color.RED);mFirstRadio.setBackgroundResource(R.drawable.tab_front_bg);mSecondRadio.setTextColor(Color.BLACK);mSecondRadio.setBackgroundColor(Color.TRANSPARENT);} else {mSecondRadio.setTextColor(Color.RED);mSecondRadio.setBackgroundResource(R.drawable.tab_front_bg);mFirstRadio.setTextColor(Color.BLACK);mFirstRadio.setBackgroundColor(Color.TRANSPARENT);}}}

问题主要出自:

mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 1, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);//如果使用下面注释掉的代码,将会出现上面说讲到的问题,当然在FirstActivity中也必须做修改(把上面代码注释掉,使用下面行代码,就会重现)//mPendingIntent2 = PendingIntent.getActivity(SecondActivity.this, 0, mIntent, 0);

看一下getActivity方法:

 PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.Parameters:context The Context in which this PendingIntent should start the activity.requestCode Private request code for the sender (currently not used).intent Intent of the activity to be launched.flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.Returns:Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.


参数requestCode用于区分Intent,如果flag为FLAG_UPDATE_CURRENT会即使更新intent中数据。

关于该方法的详细概述,敬请查看后面关于notification的源码解析,打算在周末的时候看一下notification源码,那个时候再详细分析下。


更多相关文章

  1. android中的数据存储
  2. ThreadLocal原理解析(1):数据存取
  3. Android--用JSON解析数据
  4. Android 之如何添加 android private libraries 中的包的源代码
  5. Android 7.0 之后抓包 unknown 和证书无效的解决方案(无需改代码)
  6. Android RIL代码详细分析
  7. 【Android代码片段之八】监听Android屏幕是否锁屏
  8. Android Studio 活动的最佳实践 知晓当前是在哪一个活动 随时随
  9. Android 中的数据存储

随机推荐

  1. 介绍C#中的堆和栈
  2. 介绍C#中的接口
  3. 比较C#中值类型和引用类型的区别
  4. C#/.NET易错的几点
  5. Sql的执行过程说明
  6. 比较C#和JAVA中面向对象语法的区别
  7. 有关UML的基础介绍
  8. C#中匿名对象与var以及动态类型 dynamic
  9. 比较TCP与UDP之间的区别
  10. c# webservice中访问http和https的wsdl以