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. soap in android HTTP协议实现
  2. Could not get BatchedBridge, make sure
  3. 【Android】20.4 录音
  4. 搭建Android手机系统开发环境
  5. Android(安卓)NDK安装
  6. 使用ProGuard遇到“conversion to Dalvik
  7. 在Ubuntu上下载、编译和安装Android最新
  8. Android排错:has leaked window com.andr
  9. Android切近实战(三)
  10. android animation之scale 缩放(仿微信加