之前的文章曾经介绍过Android 8.0通知适配的一些方法(详见:
Android通知栏微技巧,8.0系统中通知栏的适配),本文即是在该基础上解决一个开发过程中遇到的问题,不清楚的可以先行查看之前的博客内容,了解Android 8.0之后,通知栏相关的适配内容。

问题描述:

本文所涉及的内容是,在开发过程中,我们会遇到实时更新状态栏通知信息,比如下载的时候,下载进度信息我们需要在通知栏上进行实时的更新,结合通知相关的API我们可以通过下面的代码来实现:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    notification = new NotificationCompat.Builder(AboutusActivity.this, ConstantUtil.NOTIFICATION_CHANNELID)                                .setContentTitle("正在下载")                                .setContentText("已完成" + progress + "%")                                .setWhen(System.currentTimeMillis())                                .setSmallIcon(android.R.drawable.stat_sys_download)                                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img_download_logo))                                .setAutoCancel(true)                                .setContentIntent(contentIntent)                                .setDefaults(Notification.DEFAULT_LIGHTS)                                .build();      notificationManager.notify(notificationId, notification);

通知栏适配代码如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            String channelId = ConstantUtil.NOTIFICATION_CHANNELID;            String channelName = ConstantUtil.NOTIFICATION_CHANNELNAME;            int importance = NotificationManager.IMPORTANCE_DEFAULT;            createNotificationChannel(channelId, channelName, importance);        }@TargetApi(Build.VERSION_CODES.O)    private void createNotificationChannel(String channelId, String channelName, int importance) {        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);        NotificationManager notificationManager = (NotificationManager) getSystemService(                NOTIFICATION_SERVICE);        notificationManager.createNotificationChannel(channel);    }

通过 notificationManager.notify(notificationId, notification); 进行通知的更新,相同的notificationId会覆盖,但是在Android 8.0上会出现更新的过程中一直弹出提示音的问题,针对这个问题解决方案如下:

解决方法:

1.创建NotificationChannel时,将importance参数设置为NotificationManager.IMPORTANCE_LOW
2.更新channelId,设置为一个新的值,然后设置channel.setSound(null, null)
3.notificationBuidler.setOnlyAlertOnce(true)设置为true,这样每次只会提醒一次声音

创建NotificationChannel方法

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {     NotificationChannel channel = new NotificationChannel(ConstantUtil.NOTIFICATION_CHANNELID,   ConstantUtil.NOTIFICATION_CHANNELNAME, NotificationManager.IMPORTANCE_LOW);            channel.enableVibration(false);            channel.enableLights(true);            channel.setSound(null, null);            if (notificationManager != null)                notificationManager.createNotificationChannel(channel);     }

设置Notification方法:

notification = new NotificationCompat.Builder(AboutusActivity.this, ConstantUtil.NOTIFICATION_CHANNELID)                     .setContentTitle("正在下载")                      .setContentText("已完成" + progress + "%")                      .setWhen(System.currentTimeMillis())                      .setSmallIcon(android.R.drawable.stat_sys_download)                      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img_download_logo))                      .setAutoCancel(true)                      .setContentIntent(contentIntent)                      .setDefaults(Notification.DEFAULT_LIGHTS)                      .setOnlyAlertOnce(true)                      .build();notificationManager.notify(notificationId, notification);

通过如上设置,即可解决上述问题的出现,如有更好的解决方案欢迎留言讨论

更多相关文章

  1. Python+PyQT5的子线程更新UI界面
  2. Android(安卓)断点续传,手写多线程下载文件、数据库存储进度
  3. Android数据库更新onupgrade
  4. Android——Intent在Activity的使用详解-下(隐式Intent与实现调用
  5. 关于android混合开发模式Hybrid逻辑梳理
  6. android 某些三星手机上不显示通知Notification和消息Toast
  7. 【Tech-Android-Other】android操作sdcard中的多媒体文件——音
  8. Android(安卓)LifecycleObserver & ScheduledThreadPoolExecutor
  9. 【译】利用多线程提高程序性能(for Android)

随机推荐

  1. aapt: Android Asset Packaging Tool
  2. Android 中 RecyclerView 的基本使用
  3. Android动态添加布局
  4. Android 获取屏幕的分辨率
  5. 监听Android键盘上的按钮
  6. Android之应用首次使用的欢迎界面实例
  7. Android 技巧 - notification center 发
  8. Android中CheckBox复选框操作
  9. android下载封装类
  10. android 开发 制作弹出等待进度条