最近项目中要用Notification来显示媒体内容的下载进度,所以就看了看Notification相关内容..........
思路:创建一个NotificationService,在NotificationService中实现显示Notification和更新ProgressBar进度条内容,在程序启动时,通过activity绑定service,从而进行相关操作,代码如下:
初始化NotificationManager,remoteViews,通过NotificationManager.notify()来显示Notification,如果要更新Notification的内容,也是使用NotificationManager的notify(相同的tag,id,notification)来进行内容的
public void onCreate() {
super.onCreate();
remoteViews = new RemoteViews(this.getPackageName(), R.layout.custom_notification_button);
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
}
显示notification,并且设置相关属相,notification一般出现时会有震动和声音或者是灯光的提示(类似接受短信时),他是通过Notification的flags来设置.里边还有好多属性就不一一举例了,查阅源码就可以了,主要介绍下Notification的点击事件吧,我们一般通过Intent在Android组件之间进行跳转,但是在Notification上是使用PendingIntent来进行的,可以通过PendingIntent.getActivity(),PendingIntent.