当前位置:操作系统 > 安卓/Android >>

Android倚天剑之Notification之动感地带

  上文我们介绍怎样管理和删除通知,以及怎样实现保存用户预期导航体验的通知。本文作为Android平台Notification的最终章,我们将会给通知融入更多DIY的元素,大胆地在这把“倚天剑”上烙下自己的印记^-^。在此之前,先来看下如何在通知中显示一个进度条。

一、显示进度的通知

        通知可以包括一个动画进度指示器以显示用户正在运行的操作的进度状态。如果你能估计这种操作需要花费多长时间,可以使用“determinate”形式的指示器(一个progress bar)。如果你不能估计花费的时间,那就使用“indeterminate”形式的指示器。


1.显示一个固定的时间进度指示器

(1).技术要点

调用setProgress()方法添加进度指示器到你的通知中。

(2).代码陈列

[java] 
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
            .setContentInfo(String.valueOf(++progressNum)) 
            .setContentTitle("Picture Download") 
            .setContentText("Download in progress") 
            .setDefaults(Notification.DEFAULT_ALL) 
            .setLargeIcon(icon) 
            .setSmallIcon(R.drawable.stat_notify_gmail) 
            .setTicker("Progress Notification") 
            .setOngoing(true); 
        // 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) { 
                    builder.setProgress(100, incr, false); 
                    mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build()); 
                     
                    try { 
                        Thread.sleep(1000); 
                    } catch (InterruptedException e) { 
                        Log.d(TAG, "sleep failure"); 
                    } 
                } 
                builder.setContentText("Download complete") 
                    .setProgress(0, 0, false) 
                    .setOngoing(false); 
                mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build()); 
            } 
        }).start(); 

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentInfo(String.valueOf(++progressNum))
            .setContentTitle("Picture Download")
            .setContentText("Download in progress")
            .setDefaults(Notification.DEFAULT_ALL)
            .setLargeIcon(icon)
            .setSmallIcon(R.drawable.stat_notify_gmail)
            .setTicker("Progress Notification")
            .setOngoing(true);
        // 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) {
                    builder.setProgress(100, incr, false);
                    mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
                   
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "sleep failure");
                    }
                }
                builder.setContentText("Download complete")
                &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,