This article for you to share the Android countdown control for your reference, the specific code as follows
* * Copyright (C) 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 * Countdown control * * @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 {Super (context); Initclock(context);
Public Customdigitalclock (context, AttributeSet attrs) {Super (context, attrs);
Initclock (context);
} private void Initclock {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 *) {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) {
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 timest
R) {switch (Timestr.length ()) {case 1:timestr = "0" + timestr;
Break
return timestr;
} @Override protected void Ondetachedfromwindow () {Super.ondetachedfromwindow ();
Mtickerstopped = true;
}/** * Clock end time is on.
* * @param endtime */public void Setendtime (long endtime) {this.endtime = Endtime; }/** * pulls 12/24 mode from system Settings/private Boolean Get24hourmode () {return android.text.for Mat.
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 ();
}
}
Hopefully this article will help you learn about Android software programming.