原文:http://www.cnblogs.com/ai394495243/p/5121714.html
进度条的Notification,我们一般常见的是下载软件的时候,显示的会有一个下载进度的通知,其实这个也是很好实现的‘
在Api中是这样介绍的:
To use a progress indicator on platforms starting with Android 4.0, call setProgress()
. For previous versions, you must create your own custom notification layout that includes a ProgressBar
view.
它的意思是在android4.0以上平台,我们可以直接setProgress()的方法,但是对于之前的我们只能自定义布局。
我们来简单看一下:这个是api给的一个demo。下面我们重点看一下着色的部分
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(this); builder.setContentTitle("Picture Download") .setContentText("Download in progress") .setSmallIcon(R.mipmap.ic_launcher); 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 builder.setProgress(100, incr, false); // Displays the progress bar for the first time. mNotificationManager.notify(0, builder.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 builder.setContentText("Download complete") // Removes the progress bar .setProgress(0,0,true); mNotificationManager.notify(NOTIFICATION_FLAG, builder.build()); } } // Starts the thread by calling the run() method in its Runnable ).start();
mBuilder.setProgress(100, incr, false);和 mBuilder.setProgress(0, 0, true);的区别,如果把蓝色的部分替换为前者,在运行的时候,我们会看见是它下载完成后会给我们一个通知说下载完成,但是后者会给我们提示下载完成的同时,进度条是在运行的,不断的和滚动,也就是效果是有所不同
如下图:第一个图是下载在进行的时候,第二个是我使用后 mBuilder.setProgress(0, 0, true),下载完成的时候,我们可以发现他还存在进度条,但是一直在滚动,我这里是静态图片,大家可以运行一下看看效果,第三个图,mBuilder.setProgress(100,incr,false) ,下载完成时显示的通知