程序功能:在Notification中顯示進度條,並隨着任務完成進度更改進度條的進度,下面詳細說明:
我們要在Notification中顯示進度條,就要修改我其中的布局,首先准備一個布局文件,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/noti_tv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="15#" /> 12 13 <ProgressBar 14 android:id="@+id/noti_pd" 15 style="?android:attr/progressBarStyleHorizontal" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:layout_alignLeft="@id/noti_tv" 19 android:layout_below="@id/noti_tv" /> 20 21 </RelativeLayout>
布局很簡單,一個TextView(用來顯示進度百分比的文字描述)和一個ProgressBar(用來顯示進度條),如果想把這個布局文件和Notification相關聯,我們換需要用到RemoteViews這個類,通過RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti)就可以把noti.xml布局文件轉化為程序中的View,然后notification.contentView = view 就可以改變Notification的布局了。代碼如下:
1 private void init() { 2 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 3 notification = new Notification(R.drawable.ic_launcher, "下載", System 4 .currentTimeMillis()); 5 6 RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti); 7 notification.contentView = view; 8 9 PendingIntent contentIntent = PendingIntent.getActivity(this, 10 R.string.app_name, new Intent(), 11 PendingIntent.FLAG_UPDATE_CURRENT); 12 13 notification.contentIntent = contentIntent; 14 }
這樣我們就成功的修改了Notification的布局,我們只要給變其中TextView和ProgressBar的內容就可以實現我們的要求,要改變新布局中控件的內容,要改變TextView的內容我們需要用到notification.contentView.setTextViewText(R.id.noti_tv,"");此方法需要用到兩惡參數,其中noti_tv是我們布局文件中的TextView的Id,第二個參數是要顯示的內容;要改變ProgressBar的內容我們用到notification.contentView.setProgressBar(R.id.noti_pd, 100,int, false);此方法需要四個參數第,noti_pd為ProgressBar在布局文件中Id,第二個參數設置進度條的最大值,第三個參數是當前進度條要顯示的值,第四個參數為設置進度條是模糊的還是精准的,false是精准,true是模糊,我們應用中大部分都是精准顯示,用戶一目了然,如果設置成模糊的話,我們則看不到進度條的變化。
1 class downLoadTask extends AsyncTask<Void, Void, Void> { 2 3 @Override 4 protected Void doInBackground(Void... params) { 5 6 for (int i = 0; i <= 100; i++) { 7 8 // 為了避免頻繁發送消息所以每次增長10 9 if (i % 10 == 0) { 10 try { 11 // 模擬耗時操作 12 Thread.sleep(1000); 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 // 更改文字 17 notification.contentView.setTextViewText(R.id.noti_tv, i 18 + "%"); 19 // 更改進度條 20 notification.contentView.setProgressBar(R.id.noti_pd, 100, 21 i, false); 22 // 發送消息 23 notificationManager.notify(0, notification); 24 } 25 } 26 27 return null; 28 }
下面,只要我們在OnCreate方法中啟動downLoadTask (我們也可以放到Service去做后台耗時操作)便可以看到我們想要的結果了,詳細代碼如下:

1 public class NotiDemo extends Activity { 2 3 private NotificationManager notificationManager; 4 private Notification notification; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 9 super.onCreate(savedInstanceState); 10 11 init(); 12 // 開啟子線程 13 new downLoadTask().execute(); 14 15 } 16 17 /** 18 * 初始化 19 */ 20 private void init() { 21 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 22 notification = new Notification(R.drawable.ic_launcher, "下載", System 23 .currentTimeMillis()); 24 25 RemoteViews view = new RemoteViews(getPackageName(), R.layout.noti); 26 notification.contentView = view; 27 28 PendingIntent contentIntent = PendingIntent.getActivity(this, 29 R.string.app_name, new Intent(), 30 PendingIntent.FLAG_UPDATE_CURRENT); 31 32 notification.contentIntent = contentIntent; 33 } 34 35 /** 36 *子線程,用來更新Notification 37 */ 38 class downLoadTask extends AsyncTask<Void, Void, Void> { 39 40 @Override 41 protected Void doInBackground(Void... params) { 42 43 for (int i = 0; i <= 100; i++) { 44 45 // 為了避免頻繁發送消息所以每次增長10 46 if (i % 10 == 0) { 47 try { 48 // 模擬耗時操作 49 Thread.sleep(1000); 50 } catch (InterruptedException e) { 51 e.printStackTrace(); 52 } 53 // 更改文字 54 notification.contentView.setTextViewText(R.id.noti_tv, i 55 + "%"); 56 // 更改進度條 57 notification.contentView.setProgressBar(R.id.noti_pd, 100, 58 i, false); 59 // 發送消息 60 notificationManager.notify(0, notification); 61 } 62 } 63 64 return null; 65 } 66 } 67 68 }
效果如圖: