Android Countdown used to Countdowntimer
Android Chinese API (143)--countdowntimer
Objective
This chapter Android.os.CountDownTime chapter, version of Android 4.0 R1, translated from: "liliang1222", thank him again! Look forward to your participation in the translation of Android related information, contact me [email protected].
Statement
Welcome reprint, but please keep the original source of the article:)
Blog Park: http://www.cnblogs.com/
Android Chinese translation group: http://androidbox.sinaapp.com/
Countdowntimer
Translator Signature: liliang1222
Version: Android 4.0 R1
Structure
Inheritance relationship
Public abstract class Countdowntimer extends Object
Java.lang.Object
Android.os.CountDownTimer
class overview
Timed execution of a countdown after a period of time, during the countdown execution will be notified at a fixed interval (translator: Trigger Ontick method), the following example shows a 30s countdown in a text box:
New Countdowntimer (30000, 1000) {
public void OnTick (long millisuntilfinished) {
Mtextfield.settext ("seconds remaining:" + millisuntilfinished/1000);
}
public void OnFinish () {
Mtextfield.settext ("done!");
}
}.start ();
The Ontick call is synchronous, ensuring that the call does not occur until the previous call completes. The synchronization mechanism here is mainly used to: Ontick implementation takes a lot of time to perform more important things than the countdown interval.
constructor function
Public Countdowntimer (Long millisinfuture, long Countdowninterval)
Parameters
Millisinfuture the number of milliseconds that the OnFinish () method is called from the start Call to start () to the countdown completion. (Translator Note: Countdown time, Unit milliseconds)
Countdowninterval the time interval for receiving ontick (long) callbacks. (Translator Note: unit milliseconds)
Public methods
Public final void Cancel ()
Cancel Countdown (translator: After the cancellation, start again will restart Countdown)
public abstract void onfinish ()
Called when the countdown is complete
public abstract void OnTick (Long millisuntilfinished)
Fixed interval is called
Parameters
Millisuntilfinished countdown time remaining.
Public synchronized final Countdowntimer start ()
Start Countdown
Add
Featured Articles
Android Timer
Android Countdown feature implementation (Countdowntimer)
Sample code
Java:
|
Package Com.test.countdowntimer;
import android.app.Activity;
import Android.os.Bundle;
import Android.os.CountDownTimer;
import Android.util.Log;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.widget.Button;
import Android.widget.TextView;
import COM.TEST.R;
Public class Countdowntimeactivity extends Activity implements Onclicklistener {
TextView Mtextview;
Button MButton1;
Button MButton2;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Countdown);
Mtextview = (TextView) Findviewbyid (r.id. TextView1);
MButton1 = (Button) Findviewbyid (r.id. Button1);
MButton2 = (Button) Findviewbyid (r.id. Button2);
Mbutton1.setonclicklistener (this);
Mbutton2.setonclicklistener (this);
}
Countdowntimer timer = new countdowntimer (40000,1000) {
@Override
Public void OnTick (long millisuntilfinished) {
Mtextview.settext ("seconds remaining:" + millisuntilfinished/1000);
Try {
Thread. Sleep (1200);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Log. e ("Countdown", Millisuntilfinished+ "");
}
@Override
Public void onfinish () {
Mtextview.settext ("done! ");
}
};
@Override
Public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id. button1:
Timer.start ();
break;
Case R.id. button2:
Timer.cancel ();
break;
}
}
}
|
Xml:
|
<?xml version="1.0" encoding="Utf-8"?>
<linearlayout
xmlns:android="Http://schemas.android.com/apk/res/android"
android:orientation="Vertical"
Android:layout_width="Match_parent"
android:layout_height="Match_parent">
<textview android:text="TextView" android:id="@+id/textview1"android:layout_width= "Wrap _content "android:layout_height="wrap_content"></TextView>
<button android:text=" start " android:id="@+id/button1"android:layout_ Width= "wrap_content " android:layout_height="wrap_content" ></Button>
<button android:text=" cancel " android:id="@+id/button2"android:layout_width= "Wrap_content" android:layout_height="Wrap_content" ></Button>
</LinearLayout>
|
Description
Countdowntimer interval is 1s, we sleep in the Ontick method 1.2s, so log out to find that the print interval becomes 2s, that is, the middle of the Ontick method is not executed (will not be called before the completion of the previous call).
Source code Download
Reference: http://www.cnblogs.com/over140/archive/2011/12/20/2294220.html
Android Countdown (finishing)