欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-03 18:42:52
本文实例讲述了android实现为notification加上一个进度条的方法。分享给大家供大家参考,具体如下: package com.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相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android视图view技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android编程之activity操作技巧总结》及《android控件用法总结

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