android notification进度条,Android实现为Notification加上一个进度条的方法

本文实例讲述了android实现为notification加上一个进度条的方法。分享给大家供大家参考,具体如下:

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. */

@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, "带进条的提醒", 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);

}

@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, "进度"

+ 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);

}

};

}

}

希望本文所述对大家android程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值