Android快速实现通知栏提醒功能
日常的安卓开发过程中,经常会使用到通知栏去进行通知的功能,如何简单快速的将通知栏提醒功能集成到自己的项目之中,这篇文章将通过简短的代码展示,现在官方推荐的是这种使用builder的方式去创建通知栏的提示信息,之前的通过set
builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.icon); //设置图标
builder.setTicker("通知提示");
builder.setContentTitle("通知"); //设置标题
builder.setContentText("点击查看详细内容"); //消息内容
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光
builder.setAutoCancel(true);//打开程序后图标消失
Intent intent = new Intent(MainActivity.this, Center.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
Notification notification1 = builder.build();
notificationManager.notify(124, notification1); // 通过通知管理器发送通知
this.updateNotification = builder.getNotification();
实现代码
builder.setTicker("正在下载");
builder.setContentTitle(title); //设置标题
builder.setProgress(100, (int) totalSize * 100 / updateTotalSize + 9, false);
builder.setContentText("当前下载进度:"+(int) totalSize * 100 / updateTotalSize + "%"); //消息内容
// builder.setContentIntent(updatePendingIntent);
updateNotification = builder.getNotification();
updateNotificationManager.notify(0, updateNotification);