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;
@Override
protected 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() {
@Override
public 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;
@Override
protected 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() {
@Override
public 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;
}
@Override
public 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源码,那个时候再详细分析下。
分享到:
相关推荐
Android入门开发实例--Toast、Notification、Intent应用
Notification的用法和PendingIntent的使用
这个例子演示Android 在状态栏添加Notification信息图标及提示,相信大家对这个功能已经不陌生了,手机中安装的APP,一般都会在后台运行,时不时会在手机顶部的状态栏中显示应用的图标,滑出状态栏会看到详细的信息...
android service Notification BroadcastReceiver Intent 安装的服务开机自启动,service定时的打印消息,消息同时显示在通知栏,窗体界面的跳转,自定义控件的开发。系统广播,定时广播,轮询广播等。...
Notification
疯狂Android中有关Notification的简单例子
Notification最新用法、实现Notification的通知栏常驻、Notification的各种样式big View、解决Notification点击无效
使用javascript 封装实现 Notification 消息通知框
4.1创建自定义布局的notification 布局包含progressbar progressbar颜色可以变 自定义progressbar 布局中多个按钮可以点击 动态更新notification中的progressbar
本实例描述的Android开发中的Notification通知控件的应用,包括通知的创建、属性设置、跳转方法以及删除等。希望能给学习Notification通知的朋友带来帮助。
NULL 博文链接:https://l540151663.iteye.com/blog/2022729
Registered State Change Notification
示例功能包含普通Notification、折叠式Notification和悬挂式Notification,并且实现了Notification的显示等级。
各种Notification的使用技巧
android notification 通知 demo
NotificationDemo
Android NOtification 使用Android NOtification 使用