本文實例講述了Android編程實現通知欄進度條效果的方法。分享給大家供大家參考,具體如下:
/** * 通知管理工具類 *  * @description: * @author ldm * @date 2016-5-3 上午9:39:56 */public class NotificationUtil {  private Context mContext;  // NotificationManager : 是狀態欄通知的管理類,負責發通知、清楚通知等。  private NotificationManager manager;  // 定義Map來保存Notification對象  private Map<Integer, Notification> map = null;  public NotificationUtil(Context context) {    this.mContext = context;    // NotificationManager 是一個系統Service,必須通過 getSystemService()方法來獲取。    manager = (NotificationManager) mContext        .getSystemService(Context.NOTIFICATION_SERVICE);    map = new HashMap<Integer, Notification>();  }  public void showNotification(int notificationId) {    // 判斷對應id的Notification是否已經顯示, 以免同一個Notification出現多次    if (!map.containsKey(notificationId)) {      // 創建通知對象      Notification notification = new Notification();      // 設置通知欄滾動顯示文字      notification.tickerText = "開始下載xx文件";      // 設置顯示時間      notification.when = System.currentTimeMillis();      // 設置通知顯示的圖標      notification.icon = R.drawable.ic_launcher;      // 設置通知的特性: 通知被點擊后,自動消失      notification.flags = Notification.FLAG_AUTO_CANCEL;      // 設置點擊通知欄操作      Intent in = new Intent(mContext, MainActivity.class);// 點擊跳轉到指定頁面      PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, in,          0);      notification.contentIntent = pIntent;      // 設置通知的顯示視圖      RemoteViews remoteViews = new RemoteViews(          mContext.getPackageName(),          R.layout.notification_contentview);      // 設置暫停按鈕的點擊事件      Intent pause = new Intent(mContext, MainActivity.class);// 設置跳轉到對應界面      PendingIntent pauseIn = PendingIntent.getActivity(mContext, 0, in,          0);      // 這里可以通過Bundle等傳遞參數      remoteViews.setOnClickPendingIntent(R.id.pause, pauseIn);      /********** 簡單分隔 **************************/      // 設置取消按鈕的點擊事件      Intent stop = new Intent(mContext, MainActivity.class);// 設置跳轉到對應界面      PendingIntent stopIn = PendingIntent          .getActivity(mContext, 0, in, 0);      // 這里可以通過Bundle等傳遞參數      remoteViews.setOnClickPendingIntent(R.id.cancel, stopIn);      // 設置通知的顯示視圖      notification.contentView = remoteViews;      // 發出通知      manager.notify(notificationId, notification);      map.put(notificationId, notification);// 存入Map中    }  }  /**   * 取消通知操作   *    * @description:   * @author ldm   * @date 2016-5-3 上午9:32:47   */  public void cancel(int notificationId) {    manager.cancel(notificationId);    map.remove(notificationId);  }  public void updateProgress(int notificationId, int progress) {    Notification notify = map.get(notificationId);    if (null != notify) {      // 修改進度條      notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);      manager.notify(notificationId, notify);    }  }}布局文件notification_contentview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知欄下載測試" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:id="@+id/pBar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="4dp" android:layout_weight="1" /> <Button android:id="@+id/pause" android:layout_width="match_parent" android:layout_height="30dp" android:layout_weight="2" android:text="暫停" /> <Button android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="30dp" android:layout_weight="2" android:text="取消" /> </LinearLayout></LinearLayout>
Activity中簡單測試發通知,項目中根據需要使用,比如文件下載中要更新進度,取消時進行對應操作等。
/** * Notification是Android項目中具體的狀態欄通知對象,可以設置icon、文字、提示聲音、振動等等參數。 * 常用屬性: * icon:設置通知上顯示的圖標 * tickerText:設置通知中滾動顯示的文字  * text:設置通知的內容 * flags:設置通知的特性 * defaults:設置通知默認效果 * when:設置通知顯示的時間 * contentView:設置通知顯示的內容視圖 * sound:設置通知的聲音 * contentIntent:設置點擊通知時的跳轉等操作 *//** * 在通知欄中實現下載進度條樣式展示Demo *  * @description: * @author ldm * @date 2016-5-3 上午8:40:37 */public class MainActivity extends ActionBarActivity {  private NotificationUtil mNotificationUtil;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mNotificationUtil = new NotificationUtil(this);    findViewById(R.id.send).setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        mNotificationUtil.showNotification(100);// 測試發出通知      }    });  }}希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答