Android官方開發文檔Training系列課程中文版:通知使用者之在通知中顯示進度

來源:互聯網
上載者:User

標籤:

原文地址:http://android.xsoftlab.net/training/notify-user/display-progress.html#FixedProgress

通知中包含了一個進度列指示器,用來向使用者展示一項進行中中的工作狀態。如果你可以確保任務會花費多長時間,並且可以在任何時候得知它完成了多少工作,那麼就可以使用確定樣式的指標(一個進度條)。如果不能確定任務需要花費的時間,可以使用不確定樣式的指標(一個活動的指標)。

進度列指示器由ProgressBar類實現。

使用進度列指示器,可以調用setProgress()方法。確定樣式與不確定樣式會在下面的章節中討論。

顯示確定進度列指示器

為了顯示確定進度列指示器,需要調用setProgress(max, progress, false)方法將指標添加到通知上,然後再將該通知發布出去。該方法的第三個參數用於指示該進度條是確定性進度條(true)還是不確定性進度條(false)。隨著操作的處理,進度progress會增長,這時需要更新通知。在操作結束時,progress應該等於max。一種常規的方式是將max設定為100,然後將progress以百分比的形式自增。

你也可以選擇在任務完成的時候將進度條取消顯示或者移除通知。在前一種情況中,要記得更新通知的文本,告訴使用者任務已完成。後一種情況中,調用setProgress(0, 0, false)就可以完成通知的移除。

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 threadnew 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();

最終的效果如所示:

左邊的圖顯示了進行中中的通知,而右邊的圖顯示了任務完成後的通知。

顯示持續活動的指標

為了顯示不確定性指標,需要調用setProgress(0, 0, true)方法將進度條顯示在通知中,然後將該通知發布。第一第二個參數將會被忽略,第三個參數決定了該進度條是否是不確定性進度條。最終的顯示效果為與常規進度條有相同的顯示風格,除了它一直在動之外。

在操作開始之前請發布該通知,進度動畫會一直持續運行,直到你修改了通知。當操作完成後,調用setProgress(0, 0, false)方法然後更新通知以便移除活動指標。否則的話,就算是任務完成後,該動畫也不會停止。所以要記得在任務完成後更改通知文本,以便告知使用者操作已完成。

// Sets the progress indicator to a max value, the current completion// percentage, and "determinate" statemBuilder.setProgress(100, incr, false);// Issues the notificationmNotifyManager.notify(id, mBuilder.build());

找到前面的代碼,將下面部分替換。要記得setProgress()方法的第三個參數為true:

 // Sets an activity indicator for an operation of indeterminate lengthmBuilder.setProgress(0, 0, true);// Issues the notificationmNotifyManager.notify(id, mBuilder.build());

最終的顯示效果如下:

Android官方開發文檔Training系列課程中文版:通知使用者之在通知中顯示進度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.