Android countdown code

Source: Internet
Author: User

, Change the time every one second.

Xml: Copy codeThe Code is as follows: <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity"
Android: background = "#000000"
>
<Com. gettime. CustomDigitalClock
Android: id = "@ + id/time"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_gravity = "center"
Android: gravity = "center"
Android: textColor = "# FF0000"
Android: textSize = "45sp"/>
</RelativeLayout>
// The preceding <com. gettime. CustomDigitalClock is the complete position of the class.
MainActivity. java
Package com. gettime;
Import android. OS. Bundle;
Import android. app. Activity;
Public class MainActivity extends Activity {
Private CustomDigitalClock timeClock;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TimeClock = (CustomDigitalClock) findViewById (R. id. time );
TimeClock. setEndTime (System. currentTimeMillis () + 30*1000*24*24 );
}
}
CustomDigitalClock. java
/*
* Copyright (C) 2012 The * Project
* All right reserved.
* Version 1.00
* Author veally@foxmail.com
*/
Package com. gettime;
Import java. util. Calendar;
Import android. content. Context;
Import android. database. ContentObserver;
Import android. OS. Handler;
Import android. OS. SystemClock;
Import android. provider. Settings;
Import android. text. Html;
Import android. text. Spannable;
Import android. text. Spanned;
Import android. text. style. AbsoluteSizeSpan;
Import android. util. AttributeSet;
Import android. widget. DigitalClock;
/**
* Custom digital clock
*
* @ Author veally@foxmail.com
*/
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;
Public static long distanceTime;
Private ClockListener mClockListener;
Private static boolean isFirst;
Private boolean mTickerStopped;
@ 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 ();
}
DistanceTime = endTime-currentTime;
DistanceTime // = 1000;
If (distanceTime = 0 ){
SetText ("00:00:00 ");
OnDetachedFromWindow ();
} Else if (distanceTime <0 ){
SetText ("00:00:00 ");
OnDetachedFromWindow ();
} 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 Spanned dealTime (long time ){
Spanned str;
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 (dayStr). append ("day"). append (hoursStr). append ("Hour ")
. Append (minutesStr). append ("Minute"). append (secondStr). append ("second ");
Str = Html. fromHtml (returnString. toString ());
If (day> = 10 ){
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 2, 3,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 5, 7,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 9, 11,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 13, 14,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
} Else {
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 1, 2,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 4, 6,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 8, 10,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
(Spannable) str). setSpan (new AbsoluteSizeSpan (16), 12, 13,
Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
}
// Return returnString. toString ();
Return str;
}
/**
* 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 ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.