Android学习之路(1)通知Notification

Android学习之路(一)通知Notification这是调用通知的函数?private void showNotification(PlayWarper mPla

Android学习之路(一)通知Notification

这是调用通知的函数

?private void showNotification(PlayWarper mPlayWarper) {
??????? mNotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
??????? intent = new Intent(mContext, FileListLocalActivity.class);

????????pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
??????? RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notice);
??????? String name = mPlayWarper.musicInfo.getName();
??mNotification = new Notification(android.R.drawable.ic_media_play, name, System.currentTimeMillis());

??????? mNotification.contentIntent = pendingIntent;
??????? mNotification.contentView = rv;
??????? // mNotification.setLatestEventInfo(this, "正在播放:" + name, name,
??????? // pendingIntent);
???????? mNotification.flags |= Notification.FLAG_ONGOING_EVENT; //
??????? // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
??????? mNotification.flags |= Notification.FLAG_NO_CLEAR; //
??????? mNotification.flags |= Intent.FLAG_ACTIVITY_SINGLE_TOP;

??????? // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
?????????mNotificationManager.notify(Constant.NOTICE_ID, mNotification);
??? }

?

?

开发Notification遇到问题

?

1,点击通知无Activity返回,如同没有点击(有些人倒想实现这样的效果)

这个问题很二,哈哈,原因就是AndroidManifest.xml没有定义NoticeActivity.

?这行代码决定点击通知后跳转到哪个Activity

intent = new Intent(mContext, FileListLocalActivity.class);

?

2,点击通知后有Activity了,但是再次点击无任何操作.

这个原因是创建PendingIntent时最后一个参数flag定义不正确,应该是

PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
FLAG_ONE_SHOT是只能点击通知一次

FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,

?

3,通知内容不是定义的layout,而是文字信息

?// mNotification.setLatestEventInfo(this, "正在播放:" + name, name,
??????? // pendingIntent);
注释掉这样的代码就可以显示R.layout.notice的layout到通知里面.

?

4,点击通知后每次都新建一个Activity,不能把后台的Activity直接显示到前台

?解决方法是修改点击后出现的Activity在AndroidManifest.xml中加上launchMode,代码如下:

???? <activity
??????????? android:name=".FileListLocalActivity"
??????????? android:launchMode="singleTask" />

?

launchMode的解释相见SDK文档http://developer.android.com/reference/android/R.attr.html#launchMode

?