请大家先看这个说明:
相信大家在使用课本或者其他资料时候常常看到类似下面的代码。而且试了,挺好,下拉,点击,然后就有跳转。
但是点击后提示栏中原有图标不消失,而更深入一点发现如果要传递参数,但是传递的参数都一样,要么都是最新的,要么都是最旧的,这跟参数有关系。好了,废话不多说,几个注意点我在下面的代码中圈出来:
//声明通知(消息)管理器
NotificationManager m_NotificationManager;
Intent m_Intent;
PendingIntent m_PendingIntent;
//声明Notification对象
Notification m_Notification;
//添加通知
m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//点击通知时转移内容
m_Intent = new Intent(FileTranferService.this, AfterClick.class);
Log.e("Before Notice",desPath);
Bundle bundle = new Bundle();
bundle.putString("desPath", desPath);
m_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
m_Intent.putExtras(bundle);
//主要是设置点击通知时显示内容的类
m_PendingIntent = PendingIntent.getActivity(FileTranferService.this, Setting.NoticeID, m_Intent, PendingIntent.FLAG_CANCEL_CURRENT);
//要想参数各自不同,需要这个ID不同,在谷歌中这个参数描述可能会误导你以为这个参数无所谓而一直使用0
//构造Notification对象
m_Notification = new Notification();
//
m_Notification.contentIntent=m_PendingIntent;
//点击后自动清除,图示项只有这样设置后才能在点击后自动删除
m_Notification.flags|=Notification.FLAG_AUTO_CANCEL;
//设置通知在状态栏显示的图标
m_Notification.icon = R.drawable.download;
//当我们点击通知时显示的内容
m_Notification.tickerText = desPath.replaceFirst(temp2+"/", "")+"下载完成";
//通知时发出默认的声音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//设置通知显示的参数
m_Notification.setLatestEventInfo(FileTranferService.this,desPath.replaceFirst(temp2+"/", ""), "下载完成", m_PendingIntent);
//可以理解为执行这个通知
m_NotificationManager.notify(Setting.NoticeID++, m_Notification);
//同样,ID应该不同,这个不多说,这个容易找到相关说明
原文:http://blog.163.com/caoguoqiang_dlut/blog/static/10658914220114167219320/