欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法示例

 更新時(shí)間:2018年02月12日 11:07:56   作者:遲做總比不做強(qiáng)  
這篇文章主要介紹了Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android通知欄進(jìn)度條效果的功能、布局相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法。分享給大家供大家參考,具體如下:

/**
 * 通知管理工具類
 * 
 * @description:
 * @author ldm
 * @date 2016-5-3 上午9:39:56
 */
public class NotificationUtil {
  private Context mContext;
  // NotificationManager : 是狀態(tài)欄通知的管理類,負(fù)責(zé)發(fā)通知、清楚通知等。
  private NotificationManager manager;
  // 定義Map來保存Notification對(duì)象
  private Map<Integer, Notification> map = null;
  public NotificationUtil(Context context) {
    this.mContext = context;
    // NotificationManager 是一個(gè)系統(tǒng)Service,必須通過 getSystemService()方法來獲取。
    manager = (NotificationManager) mContext
        .getSystemService(Context.NOTIFICATION_SERVICE);
    map = new HashMap<Integer, Notification>();
  }
  public void showNotification(int notificationId) {
    // 判斷對(duì)應(yīng)id的Notification是否已經(jīng)顯示, 以免同一個(gè)Notification出現(xiàn)多次
    if (!map.containsKey(notificationId)) {
      // 創(chuàng)建通知對(duì)象
      Notification notification = new Notification();
      // 設(shè)置通知欄滾動(dòng)顯示文字
      notification.tickerText = "開始下載xx文件";
      // 設(shè)置顯示時(shí)間
      notification.when = System.currentTimeMillis();
      // 設(shè)置通知顯示的圖標(biāo)
      notification.icon = R.drawable.ic_launcher;
      // 設(shè)置通知的特性: 通知被點(diǎn)擊后,自動(dòng)消失
      notification.flags = Notification.FLAG_AUTO_CANCEL;
      // 設(shè)置點(diǎn)擊通知欄操作
      Intent in = new Intent(mContext, MainActivity.class);// 點(diǎn)擊跳轉(zhuǎn)到指定頁面
      PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, in,
          0);
      notification.contentIntent = pIntent;
      // 設(shè)置通知的顯示視圖
      RemoteViews remoteViews = new RemoteViews(
          mContext.getPackageName(),
          R.layout.notification_contentview);
      // 設(shè)置暫停按鈕的點(diǎn)擊事件
      Intent pause = new Intent(mContext, MainActivity.class);// 設(shè)置跳轉(zhuǎn)到對(duì)應(yīng)界面
      PendingIntent pauseIn = PendingIntent.getActivity(mContext, 0, in,
          0);
      // 這里可以通過Bundle等傳遞參數(shù)
      remoteViews.setOnClickPendingIntent(R.id.pause, pauseIn);
      /********** 簡單分隔 **************************/
      // 設(shè)置取消按鈕的點(diǎn)擊事件
      Intent stop = new Intent(mContext, MainActivity.class);// 設(shè)置跳轉(zhuǎn)到對(duì)應(yīng)界面
      PendingIntent stopIn = PendingIntent
          .getActivity(mContext, 0, in, 0);
      // 這里可以通過Bundle等傳遞參數(shù)
      remoteViews.setOnClickPendingIntent(R.id.cancel, stopIn);
      // 設(shè)置通知的顯示視圖
      notification.contentView = remoteViews;
      // 發(fā)出通知
      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) {
      // 修改進(jìn)度條
      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="通知欄下載測(cè)試" />
  <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中簡單測(cè)試發(fā)通知,項(xiàng)目中根據(jù)需要使用,比如文件下載中要更新進(jìn)度,取消時(shí)進(jìn)行對(duì)應(yīng)操作等。

/**
 * Notification是Android項(xiàng)目中具體的狀態(tài)欄通知對(duì)象,可以設(shè)置icon、文字、提示聲音、振動(dòng)等等參數(shù)。
 * 常用屬性:
 * icon:設(shè)置通知上顯示的圖標(biāo)
 * tickerText:設(shè)置通知中滾動(dòng)顯示的文字 
 * text:設(shè)置通知的內(nèi)容
 * flags:設(shè)置通知的特性
 * defaults:設(shè)置通知默認(rèn)效果
 * when:設(shè)置通知顯示的時(shí)間
 * contentView:設(shè)置通知顯示的內(nèi)容視圖
 * sound:設(shè)置通知的聲音
 * contentIntent:設(shè)置點(diǎn)擊通知時(shí)的跳轉(zhuǎn)等操作
 */
/**
 * 在通知欄中實(shí)現(xiàn)下載進(jìn)度條樣式展示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);// 測(cè)試發(fā)出通知
      }
    });
  }
}

Android通知功能可以參考前面一篇http://www.dbjr.com.cn/article/85807.htm

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論