原文: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) ,下载完成时显示的通知


更多相关文章

  1. android 按钮点击效果 imageview button imagebutton均可
  2. Android(安卓)不支持 SYSV IPC (SYSV IPC)
  3. Android(安卓)studio升级AndroidX
  4. android工程下轻松运行java main程序
  5. android app应用内更新
  6. android后台运行服务,加验证
  7. Android(安卓)用axis2 webService产生java.lang.ClassCastExcept
  8. 开始使用Android(安卓)Sutdio(三)创建一个Hello World程序
  9. Android消息通知

随机推荐

  1. 第一部分:如何在iOS和Android上选择一个J
  2. Android SharePreferences源码解析
  3. Android 开发 之 折叠布局(CollapsingTool
  4. 网页 Android(安卓)套壳
  5. 20180505_android传感器种类及获取
  6. 【Android】Navigation实现页面跳转
  7. android2.2中默认浏览器无法使用手势放大
  8. android tips—NumberPicker,DataPicker,Ti
  9. Android 判断应用是否开启了通知权限
  10. Android图形架构分析总结