Android實(shí)現(xiàn)為Notification加上一個(gè)進(jìn)度條的方法
本文實(shí)例講述了Android實(shí)現(xiàn)為Notification加上一個(gè)進(jìn)度條的方法。分享給大家供大家參考,具體如下:
package com.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; import android.widget.Toast; public class nofificationActivity extends Activity implements OnClickListener { private static final int NOTIFICATION_ID = 0x12; private Notification notification = null; private NotificationManager manager = null; public Handler handler; private int _progress = 0; private Thread thread = null; private boolean isStop = false; // 當(dāng)界面處理停止的狀態(tài) 時(shí),設(shè)置讓進(jìn)度條取消 @Override protected void onPause() { // TODO Auto-generated method stub isStop = false; manager.cancel(NOTIFICATION_ID); super.onPause(); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.Button01); btn.setOnClickListener(this); notification = new Notification(R.drawable.icon, "帶進(jìn)條的提醒", System .currentTimeMillis()); notification.icon = R.drawable.icon; // 通過RemoteViews 設(shè)置notification中View 的屬性 notification.contentView = new RemoteViews(getApplication() .getPackageName(), R.layout.custom_dialog); notification.contentView.setProgressBar(R.id.pb, 100, 0, false); notification.contentView.setTextViewText(R.id.tv, "進(jìn)度" + _progress + "%"); // 通過PendingIntetn // 設(shè)置要跳往的Activity,這里也可以設(shè)置發(fā)送一個(gè)服務(wù)或者廣播, // 不過在這里的操作都必須是用戶點(diǎn)擊notification之后才觸發(fā)的 notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, remoteView.class), 0); // 獲得一個(gè)NotificationManger 對象,此對象可以對notification做統(tǒng)一管理,只需要知道ID manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public void onClick(View v) { // TODO Auto-generated method stub isStop = true; manager.notify(NOTIFICATION_ID, notification); thread = new Thread(new Runnable() { @Override public void run() { Thread.currentThread(); // TODO Auto-generated method stub while (isStop) { _progress += 10; Message msg = handler.obtainMessage(); msg.arg1 = _progress; msg.sendToTarget(); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); thread.start(); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub notification.contentView.setProgressBar(R.id.pb, 100, msg.arg1, false); notification.contentView.setTextViewText(R.id.tv, "進(jìn)度" + msg.arg1 + "%"); manager.notify(NOTIFICATION_ID, notification); if (msg.arg1 == 100) { _progress = 0; manager.cancel(NOTIFICATION_ID); isStop = false; Toast.makeText(nofificationActivity.this, "下載完畢", 1000) .show(); } super.handleMessage(msg); } }; } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android WebView實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android WebView實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android 中 Fragment 嵌套 Fragment使用存在的bug附完美解決方案
本文從兩個(gè)方面淺析Android 中 Fragment 嵌套 Fragment使用存在的bug問題,原因找到就可以完美的解決了,對fragment嵌套fragment使用相關(guān)知識感興趣的朋友一起看看吧2016-08-08android實(shí)現(xiàn)攜程購票起始點(diǎn)位置交換
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)攜程購票起始點(diǎn)位置交換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06淺談Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能
這篇文章主要介紹了Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能,需要的朋友可以參考下2017-05-05Android中Activity之間跳轉(zhuǎn)和參數(shù)傳遞的實(shí)例
本篇文章主要介紹了Android中Activity之間跳轉(zhuǎn)和參數(shù)傳遞的實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android NDK 開發(fā)中 SO 包大小壓縮方法詳解
這篇文章主要為為大家介紹了Android NDK 開發(fā)中 SO 包大小壓縮方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android開發(fā)實(shí)現(xiàn)刪除聯(lián)系人通話記錄的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)刪除聯(lián)系人通話記錄的方法,較為詳細(xì)的分析了Android刪除通話記錄的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10