Notification相关和一些关键点收集
最近项目需求写一个自定义Notification,将其中的一些知识总结下,方便自己回顾,也给其他人一些参考,个人理解可能有误,欢迎大家交流。
Notification官方说明和使用指导先放上来
- 官方说明:
http://developer.android.com/reference/android/app/Notification.html - 官方指导:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Notification的分类
- 默认样式的Notification,API=1就引入
- 扩展Notification,从Android 4.3引入
- 自定义Notification(将自定义的RemotveViews传入Notification的ContentView)
生成默认Notification
- icon属性必须设置,避免Notification不被系统显示
- API=11及以前:
public Notification() {
this.when = System.currentTimeMillis();
}
和
public Notification(int icon, CharSequence tickerText, long when) {
this.icon = icon;
this.tickerText = tickerText;
this.when = when;
}
然后使用
public void setLatestEventInfo(Context context,
CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
RemoteViews contentView = new RemoteViews(context.getPackageName(), com.android.internal.R.layout.status_bar_latest_event_content);
if (this.icon != 0) {
contentView.setImageViewResource(com.android.internal.R.id.icon, this.icon);
}
if (contentTitle != null) { contentView.setTextViewText(com.android.internal.R.id.title, contentTitle);
}
if (contentText != null) { contentView.setTextViewText(com.android.internal.R.id.text, contentText);
}
if (this.when != 0) {
Date date = new Date(when);
CharSequence str =
DateUtils.isToday(when) ? DateFormat.getTimeFormat(context).format(date) DateFormat.getDateFormat(context).format(date); contentView.setTextViewText(com.android.internal.R.id.time, str);
}
this.contentView = contentView;
this.contentIntent = contentIntent;
}
生成默认的Notification样式,然而在API11时被标记为decepate在api23时被移除
3.0以后
Notification notification = new Notification.Builder.getNotification();
获取基本的Notification对象4.3版本以后
Notification notification = new Notification.Builder.builer();
可扩展Notification
// XXX TODO 后续补充
自定义Notification
- 自定义Notification的主要操作在于生成正确的RemoteViews并设置合适的点击点击响应。(由于在Android 3.0以前不支持自定义RemoteViews中的组件响应,所以常见的需要操作的自定义Notification都会隐藏,eg.酷狗音乐等等)
- RemoteViews只支持系统提供的基础组件,如果包含自定义View会报错。背景色可以使用一张充满的ImageView实现。在Notification RemoteViews中如果最外层为LinearLayout会无法实现对内容的居中。布局样例如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/background_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/notice_toggle_one_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background=
"@drawable/notification_toggle_click_selector"
android:clickable="true">
<ImageView
android:id="@+id/notice_toggle_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src=
"@drawable/notification_toggle_icon"
/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
- remoteViews.setImageViewResource(R.id.
background_img,R.drawable.background)和
remoteViews.setOnClickPendingIntent(R.id.notice_click_id, pendingIntent)实现对RemoteViews的图片配置和点击响应配置
展示Notification
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Intent.notification_service);
mNotificationManager.notify(id,notification);
程序公用一套ID,需要区分各个Notification,避免在后续更新时造成错误,建议为自己的application建立一个Notification Id的常数类
更新Notification
mNotificationManager.notify(id,notification);
取消Notification
- 可以设置FLAG
- 使用代码
mNotificationManager.cancel(id);
或则
mNotificationManager.cancelAll();
如果手动设置了Notification的Notification.FLAG_FOREGROUND_SERVICE会导致NotificationManager.cancel(id)或则cancelAll()方法不生效
Notification的作用
- Notification Toggle 常用快捷快关,便于随时操作
- 设置为前台服务,避免后台服务被杀死
前台服务
- 作用:避免后台服务被杀死,提高程序的存活率
- 开启前台服务:service.startFrontService(id,notification);id不能为0
- 关闭前台服务:service.stopFrontService(boolean);true:删除前台NOtification;false:不删除Notification,等待后续的NotificationManager调用cancel()方法
- 在4.3版本以前可以直接使用一个空的Notification开启其前台服务startForegroundService(id,new Notification())
TAG
- Notification的when属性决定了Notification在通知栏的位置,在when相等的情况下,后续notify()排在前面
- 附录一个详尽的介绍博文:
http://blog.csdn.net/vipzjyno1/article/details/25248021