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;
// 当界面处理停止的状态 时,设置让进度条取消
@override
protected void onpause() {
// todo auto-generated method stub
isstop = false;
manager.cancel(notification_id);
super.onpause();
}
/** called when the activity is first created. */
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, "带进条的提醒", system
.currenttimemillis());
notification.icon = r.drawable.icon;
// 通过remoteviews 设置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, "进度" + _progress
+ "%");
// 通过pendingintetn
// 设置要跳往的activity,这里也可以设置发送一个服务或者广播,
// 不过在这里的操作都必须是用户点击notification之后才触发的
notification.contentintent = pendingintent.getactivity(this, 0,
new intent(this, remoteview.class), 0);
// 获得一个notificationmanger 对象,此对象可以对notification做统一管理,只需要知道id
manager = (notificationmanager) getsystemservice(notification_service);
public void onclick(view v) {
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() {
public void handlemessage(message msg) {
notification.contentview.setprogressbar(r.id.pb, 100, msg.arg1,
false);
notification.contentview.settextviewtext(r.id.tv, "进度"
+ 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);
};
}