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

android倒计时控件示例

程序员文章站 2024-03-31 14:21:10
本文为大家分享了android倒计时控件,供大家参考,具体代码如下 /* * copyright (c) 2012 the * project * all...

本文为大家分享了android倒计时控件,供大家参考,具体代码如下

/*
 * copyright (c) 2012 the * project
 * all right reserved.
 * version 1.00 2012-2-11
 * author veally@foxmail.com
 */
package com.ly.sxh.view;
 
import android.content.context;
import android.database.contentobserver;
import android.os.handler;
import android.os.systemclock;
import android.provider.settings;
import android.util.attributeset;
import android.widget.digitalclock;
 
import java.util.calendar;
 
/**
 * custom digital clock
 * 倒计时控件
 *
 * @author
 */
public class customdigitalclock extends digitalclock {
 
  calendar mcalendar;
  private final static string m12 = "h:mm aa";
  private final static string m24 = "k:mm";
  private formatchangeobserver mformatchangeobserver;
 
  private runnable mticker;
  private handler mhandler;
  private long endtime;
  private clocklistener mclocklistener;
 
  private boolean mtickerstopped = false;
 
  @suppresswarnings("unused")
  private string mformat;
 
  public customdigitalclock(context context) {
    super(context);
    initclock(context);
  }
 
  public customdigitalclock(context context, attributeset attrs) {
    super(context, attrs);
    initclock(context);
  }
 
  private void initclock(context context) {
 
    if (mcalendar == null) {
      mcalendar = calendar.getinstance();
    }
 
    mformatchangeobserver = new formatchangeobserver();
    getcontext().getcontentresolver().registercontentobserver(settings.system.content_uri, true, mformatchangeobserver);
 
    setformat();
  }
 
  @override
  protected void onattachedtowindow() {
    mtickerstopped = false;
    super.onattachedtowindow();
    mhandler = new handler();
 
    /**
     * requests a tick on the next hard-second boundary
     */
    mticker = new runnable() {
      public void run() {
        if (mtickerstopped)
          return;
        long currenttime = system.currenttimemillis();
        if (currenttime / 1000 == endtime / 1000 - 5 * 60) {
          mclocklistener.remainfiveminutes();
        }
        long distancetime = endtime - currenttime;
        distancetime /= 1000;
        if (distancetime == 0) {
          settext("00:00:00");
          ondetachedfromwindow();
          mclocklistener.timeend();
        } else if (distancetime < 0) {
          settext("00:00:00");
        } else {
          settext(dealtime(distancetime));
        }
        invalidate();
        long now = systemclock.uptimemillis();
        long next = now + (1000 - now % 1000);
        mhandler.postattime(mticker, next);
      }
    };
    mticker.run();
  }
 
  /**
   * deal time string
   *
   * @param time
   * @return
   */
  public static string dealtime(long time) {
    stringbuffer returnstring = new stringbuffer();
    long day = time / (24 * 60 * 60);
    long hours = (time % (24 * 60 * 60)) / (60 * 60);
    long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60;
    long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60;
    string daystr = string.valueof(day);
    string hoursstr = timestrformat(string.valueof(hours));
    string minutesstr = timestrformat(string.valueof(minutes));
    string secondstr = timestrformat(string.valueof(second));
 
    returnstring.append(hoursstr).append(":").append(minutesstr).append(":").append(secondstr);
    return returnstring.tostring();
  }
 
  /**
   * format time
   *
   * @param timestr
   * @return
   */
  private static string timestrformat(string timestr) {
    switch (timestr.length()) {
      case 1:
        timestr = "0" + timestr;
        break;
    }
    return timestr;
  }
 
  @override
  protected void ondetachedfromwindow() {
    super.ondetachedfromwindow();
    mtickerstopped = true;
  }
 
  /**
   * clock end time from now on.
   *
   * @param endtime
   */
  public void setendtime(long endtime) {
    this.endtime = endtime;
  }
 
  /**
   * pulls 12/24 mode from system settings
   */
  private boolean get24hourmode() {
    return android.text.format.dateformat.is24hourformat(getcontext());
  }
 
  private void setformat() {
    if (get24hourmode()) {
      mformat = m24;
    } else {
      mformat = m12;
    }
  }
 
  private class formatchangeobserver extends contentobserver {
    public formatchangeobserver() {
      super(new handler());
    }
 
    @override
    public void onchange(boolean selfchange) {
      setformat();
    }
  }
 
  public void setclocklistener(clocklistener clocklistener) {
    this.mclocklistener = clocklistener;
  }
 
  public interface clocklistener {
    void timeend();
 
    void remainfiveminutes();
  }
 
}

希望本文所述对大家学习android软件编程有所帮助。