前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于通知的一些小问题

关于通知的一些小问题

作者头像
夏洛克的猫
发布2018-10-18 12:02:53
7360
发布2018-10-18 12:02:53
举报
文章被收录于专栏:移动开发移动开发

最近做下载,有一个通知显示进度的需求。这个过程中碰到一些问题,这里总结下,方便大家排查。

1.通知中下载完成后,进度条没有正确关闭掉。

我们先看官网教程中显示通知的代码:

int id = 1;
...
mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(id, mBuilder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
            }
            // When the loop is finished, updates the notification
            mBuilder.setContentText("Download complete")
            // Removes the progress bar
                    .setProgress(0,0,false);
            mNotifyManager.notify(id, mBuilder.build());
        }
    }
// Starts the thread by calling the run() method in its Runnable
).start();

进度通知比较频繁我们最好要放在子线程中操作中去,而且不能更新的太频繁,界面会有有些卡顿,甚至就是不动。我的建议是 500ms-1000ms 之间。如果频率太快,系统会直接丢弃一些更新。你们可以自己把 demo 中线程睡眠的代码去掉。观察一下现象。

如果你想要保证通知进度条正确关闭,一要保证关闭通知进度条的代码是最后执行的,尤其你是采用多线程操作的话。二是要与上一条更新有一定的时间间隔。

2.通知中的小图标没有正确显示
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
        .setSmallIcon(R.drawable.ic_notification)//small icon
        .setContentText(mContext.getString(R.string.start_download))
        .setContentTitle(mContext.getString(R.string.app_name))
        .setProgress(100, progress, false)
        .setWhen(when);

在高版本上系统上,可能会出现这种情况。小图标没有显示,而是显示一个灰色的小方块。 可以参考下图:

参考.png
参考.png

这一般是你的小图标没有按照规范尺寸去设计。具体请看 规范

关于小图标我建议你使用白色的,这样如果你在高版本中让状态栏的图标变为深色的时候,系统也可以帮你把小图标变色。如果了用了其他颜色,可能就不能变色了,状态栏上的图标颜色就不统一了。

3.通知内容太长没有换行

通知内容如果太长,可以采用如下方式:

NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        style.bigText(mContext.getString(R.string.desc_guide));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(mContext.getString(R.string.desc_insta_save_running))
                .setContentText(mContext.getString(R.string.desc_guide))
                .setContentIntent(contentIntent)
                .setStyle(style);

重点是 BigTextStyle 的使用,具体的 api 使用,大家可去看官方文档。

4.通知内容位置不固定,更新 progress 时 多条通知位置会经常变动

这是通知按照更新时间来排序的。如果你想要固定不动,可以给对应的通知传入一个固定的时间值。

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
        .setSmallIcon(R.drawable.ic_notification)//small icon
        .setContentText(mContext.getString(R.string.start_download))
        .setContentTitle(mContext.getString(R.string.app_name))
        .setProgress(100, progress, false)
        .setWhen(when);// 传入一个固定的时间值
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.通知中下载完成后,进度条没有正确关闭掉。
  • 2.通知中的小图标没有正确显示
  • 3.通知内容太长没有换行
  • 4.通知内容位置不固定,更新 progress 时 多条通知位置会经常变动
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档