W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫(xiě):fastcome1985 - 原文:http://developer.android.com/training/notify-user/display-progress.html
Notifications可以包含一個(gè)展示用戶正在進(jìn)行的操作狀態(tài)的動(dòng)畫(huà)進(jìn)度指示器。如果你可以在任何時(shí)候估算這個(gè)操作得花多少時(shí)間以及當(dāng)前已經(jīng)完成多少,你可以用“determinate(確定的,下同)”形式的指示器(一個(gè)進(jìn)度條)。如果你不能估算這個(gè)操作的長(zhǎng)度,使用“indeterminate(不確定,下同)”形式的指示器(一個(gè)活動(dòng)的指示器)。
進(jìn)度指示器用ProgressBar平臺(tái)實(shí)現(xiàn)類來(lái)顯示。
使用進(jìn)度指示器,可以調(diào)用 setProgress()方法。determinate 與 indeterminate形式將在下面的章節(jié)中介紹。
為了展示一個(gè)確定長(zhǎng)度的進(jìn)度條,調(diào)用 setProgress(max, progress, false))方法將進(jìn)度條添加進(jìn)notification,然后發(fā)布這個(gè)notification,第三個(gè)參數(shù)是個(gè)boolean類型,決定進(jìn)度條是 indeterminate (true) 還是 determinate (false)。在你操作進(jìn)行時(shí),增加progress,更新notification。在操作結(jié)束時(shí),progress應(yīng)該等于max。一個(gè)常用的調(diào)用 setProgress())的方法是設(shè)置max為100,然后增加progress就像操作的“完成百分比”。
當(dāng)操作完成的時(shí)候,你可以選擇或者讓進(jìn)度條繼續(xù)展示,或者移除它。無(wú)論哪種情況下,記得更新notification的文字來(lái)顯示操作完成。移除進(jìn)度條,調(diào)用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 thread
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
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();
結(jié)果notifications顯示在圖1中,左邊是操作正在進(jìn)行中的notification的快照,右邊是操作已經(jīng)完成的notification的快照。
圖1 操作正在進(jìn)行中與完成時(shí)的進(jìn)度條
為了展示一個(gè)持續(xù)的(indeterminate)活動(dòng)的指示器,用setProgress(0, 0, true))方法把指示器添加進(jìn)notification,然后發(fā)布這個(gè)notification 。前兩個(gè)參數(shù)忽略,第三個(gè)參數(shù)決定indicator 還是 indeterminate。結(jié)果是指示器與進(jìn)度條有同樣的樣式,除了它的動(dòng)畫(huà)正在進(jìn)行。
在操作開(kāi)始的時(shí)候發(fā)布notification,動(dòng)畫(huà)將會(huì)一直進(jìn)行直到你更新notification。當(dāng)操作完成時(shí),調(diào)用 setProgress(0, 0, false)) 方法,然后更新notification來(lái)移除這個(gè)動(dòng)畫(huà)指示器。一定要這么做,否責(zé)即使你操作完成了,動(dòng)畫(huà)還是會(huì)在那運(yùn)行。同時(shí)也要記得更新notification的文字來(lái)顯示操作完成。
為了觀察持續(xù)的活動(dòng)的指示器是如何工作的,看前面的代碼。定位到下面的幾行:
// Sets the progress indicator to a max value, the current completion
// percentage, and "determinate" state
mBuilder.setProgress(100, incr, false);
// Issues the notification
mNotifyManager.notify(id, mBuilder.build());
將你找到的代碼用下面的幾行代碼代替,注意 setProgress()方法的第三個(gè)參數(shù)設(shè)置成了true,表示進(jìn)度條是 indeterminate類型的。
// Sets an activity indicator for an operation of indeterminate length
mBuilder.setProgress(0, 0, true);
// Issues the notification
mNotifyManager.notify(id, mBuilder.build());
結(jié)果顯示在圖2中:
圖2 正在進(jìn)行的活動(dòng)的指示器
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: