Notification带进度条的通知 Android | 您所在的位置:网站首页 › 安卓滚动条下载 › Notification带进度条的通知 Android |
带进度条的通知 Android
Notification 或者现在叫NotificationCompat 设置通知内容 首先,您需要使用 NotificationCompat.Builder 对象设置通知内容和渠道。以下示例显示了如何创建包含下列内容的通知: 小图标,通过 setSmallIcon() 设置。这是所必需的唯一用户可见内容。 标题,通过 setContentTitle() 设置。 正文文本,通过 setContentText() 设置。 通知优先级,通过 setPriority() 设置。优先级确定通知在 Android 7.1 和更低版本上的干扰程度。 Android 8.0 和更高版本,必须设置渠道,否则不显示 首先定义一个通知 //CHANNEL_ID 自己定义 val CHANNEL_ID="xxx.xxxxxxx.xxx" //名称会显示 在通知的类别上 打开应用通知设置是可以看到这个名称 val CHANNEL_NAME="上传服务" var builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("上传视频...") .setContentText("剩余个数...") .setPriority(NotificationCompat.PRIORITY_DEFAULT) //这个级别如果设置的太低需要在通知设置才能显示通知 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//android O 及以上需要创建通知渠道 val name = getString(R.string.channel_name) val descriptionText = getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_DEFAULT//级别会影响使用到的通知显示 val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { description = descriptionText } } // 注册渠道 val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) NotificationManagerCompat.notify() //显示通知调用 NotificationManagerCompat.notify(),并将通知的唯一 ID 和NotificationCompat.Builder.build() //nitifyid 你调用更新取消的id val notifyid =3001 //显示 notificationManager.notify(notifyid,builder.builder()) //取消 notificationManager.notify.cancle(notifyid) //更新 如 显示操作 ...如果需要显示进度条 int PROGRESS_MAX = 100; int PROGRESS_CURRENT = 0; //第三个参数表示是否是确定的 false 则为波动的进度条 是的话为确定的数值 比如说10% notification.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);效果图: //描述文字不能发太长哈 不让就像我这样显示不下了 |
CopyRight 2018-2019 实验室设备网 版权所有 |