Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法示例
本文實(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)文章
Android OnCreate()中獲取控件高度與寬度兩種方法詳解
這篇文章主要介紹了Android OnCreate()中獲取控件高度與寬度兩種方法詳解的相關(guān)資料,這里提供了兩種方法,大家可以都看下,需要的朋友可以參考下2016-12-12Android AIDL——進(jìn)程通信機(jī)制詳解
這篇文章主要介紹了Android AIDL——進(jìn)程通信機(jī)制詳解的相關(guān)資料,并附簡單實(shí)例,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10android上的一個(gè)網(wǎng)絡(luò)接口和圖片緩存框架enif簡析
android上的一個(gè)網(wǎng)絡(luò)接口和圖片緩存框架enif詳細(xì)介紹:底層網(wǎng)絡(luò)接口采用apache的httpclient連接池框架、圖片緩存采用基于LRU的算法等等,需要了解的朋友可以詳細(xì)參考下2012-12-12Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄樣式
這篇文章主要介紹了Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄效果的相關(guān)資料,需要的朋友可以參考下2017-01-01Android setButtonDrawable()的兼容問題解決辦法
這篇文章主要介紹了Android setButtonDrawable()的兼容問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03android通過servlet服務(wù)器保存文件到手機(jī)
這篇文章主要為大家詳細(xì)介紹了android通過servlet服務(wù)器保存文件到手機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Android Flutter利用貝塞爾曲線畫一個(gè)小海豚
貝塞爾曲線的應(yīng)用填補(bǔ)了計(jì)算機(jī)繪制與手繪之前的差距,更能表達(dá)人想畫出的曲線。本文就將利用貝塞爾曲線繪制一個(gè)可愛的小海豚,需要的可以參考一下2022-04-04