小白日记——下载进度的顶部通知

来源:互联网 发布:老实男人知乎 编辑:程序博客网 时间:2024/04/28 09:32

技术小白,把学到的觉得有用的东西放在上面,希望不要见笑。如果需要,欢迎转载。

带有进度条的顶部通知

声明全局变量

private PendingIntent pendingIntent;private NotificationManager notificationManager = null;private NotificationCompat.Builder builder;

在使用的地方实例化

notificationManager = (NotificationManager) .getSystemService(Context.NOTIFICATION_SERVICE);builder = new NotificationCompat.Builder(this);Intent intent = new Intent(this, NetActivity.class);pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);

设置顶部通知的样式

builder.setSmallIcon(R.mipmap.ic_launcher);builder.setContentTitle("下载顶部通知");builder.setContentText("带有进度条的顶部通知,增加用户体验度");

程序演示,模拟进度条进度

new Thread(new Runnable(){        @Override         public void run() {            for (int i = 0; i <= 100; i++) {                builder.setProgress(100, i, false);                notificationManager.notify(13, builder.build());                try {                    Thread.sleep(300);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            builder.setContentText("下载完毕");            notificationManager.notify(14, builder.build());            notificationManager.cancel(13);        }    }).start();

刚刚学习使用MarkDown编辑器,勿喷,谢谢

0 0
原创粉丝点击