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;
// 通過(guò)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
+ "%");
// 通過(guò)PendingIntetn
// 設(shè)置要跳往的Activity,這里也可以設(shè)置發(fā)送一個(gè)服務(wù)或者廣播,
// 不過(guò)在這里的操作都必須是用戶點(diǎn)擊notification之后才觸發(fā)的
notification.contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, remoteView.class), 0);
// 獲得一個(gè)NotificationManger 對(duì)象,此對(duì)象可以對(duì)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)容感興趣的讀者可查看本站專(zhuān)題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android實(shí)現(xiàn)登陸界面的記住密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登陸界面的記住密碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android Map新用法:MapFragment應(yīng)用介紹
MapView ,MapActivity 這種的局限在于,必須要繼承MapActivity,否則無(wú)法使用MapView,但是,MapFragment 這種的局限在于,必須要安裝Google Play Service ,也就是說(shuō)必須是原生rom。而且sdk要在12以上2013-01-01
Android開(kāi)發(fā)vsts?agent支持自定義task過(guò)程詳解
這篇文章主要介紹了Android開(kāi)發(fā)vsts?agent支持自定義task過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)
這篇文章主要介紹了如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo) 很多朋友反映剛升級(jí)了Android studio 3.0,發(fā)現(xiàn)光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)沒(méi)有了,下文給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-11-11
Android富文本實(shí)現(xiàn)的幾種方式匯總
由于項(xiàng)目中需要使用到富文本顯示和編輯,索性整理下,這篇文章主要給大家介紹了關(guān)于Android富文本實(shí)現(xiàn)的幾種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Recycleview實(shí)現(xiàn)無(wú)限自動(dòng)輪播
這篇文章主要為大家詳細(xì)介紹了Recycleview實(shí)現(xiàn)無(wú)限自動(dòng)輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android Studio做超好玩的拼圖游戲 附送詳細(xì)注釋源碼
這篇文章主要介紹了用Android Studio做的一個(gè)超好玩的拼圖游戲,你是0基礎(chǔ)Android小白也能包你學(xué)會(huì),另外附送超詳細(xì)注釋的源碼,建議收藏!2021-08-08

