使用java類定時器,定時器任務和android處理程序。
接口welcome_activity.xml
& lt?xml版本="1.0 "編碼="utf-8 "?& gt
& ltrelative layout xmlns:Android = "/apk/RES/Android "
xmlns:tools="/tools "
Android:layout _ width = " match _ parent "
Android:layout _ height = " match _ parent " & gt;
& lt文本視圖
android:id="@+id/count_down "
android:layout_width="60dp "
android:layout_height="60dp "
Android:layout _ alignParentRight = " true "
Android:layout _ alignParentTop = " true "
Android:layout _ margin right = " 16dp "
Android:layout _ margin top = " 16dp "
安卓:重力= "中心"
android:textSize="32sp "
android:textColor="#50000000 "
Android:background = " @ drawable/count _ down _ background "
工具:text="2 "
/& gt;
& lt/relative layout & gt;
包com . example . count timer;
導入Java . util . date;
導入Java . util . timer;
導入Java . util . timertask;
導入Android . app . activity;
導入Android . content . intent;
導入Android . OS . bundle;
導入Android . OS . handler;
導入Android . view . menu;
導入Android . view . menuitem;
導入Android . view . window;
導入Android . widget . textview;
公開課WelcomeActivity拓展活動{
private final靜態int COUNT = 1;
私人TextView倒計時;
@覆蓋
受保護的void onCreate(Bundle saved instancestate){
super . oncreate(savedInstanceState);
requestWindowFeature(窗口。特征_編號_標題);
setContentView(r . layout . activity _ welcome);
init view();
}
//s hedule的第壹個參數是第壹次啟動延遲的時間,第三個參數是執行的頻率。單位都是ms .//所以這裏是向處理程序發送消息,每秒更新壹次UI。//三秒鐘後,在定時器的第二個時間表中,是時候跳轉到另壹個界面了。
私有void initView() {
count down =(TextView)findViewById(r . id . count _ down);
最終定時器timer =新定時器();
final long end = system . current time millis()+1000 * 3;
timer.schedule(新TimerTask() {
@覆蓋
公共無效運行(){
handler.sendEmptyMessage(計數);
}
}, 0, 1000);
//這裏schedule的第二個參數含義是在此時盡快運行run中的方法。
timer.schedule(新TimerTask() {
@覆蓋
公共無效運行(){
Intent I = new Intent(welcome activity . this,second activity . class);
i.addFlags(Intent。旗標_活動_清除_任務);
開始活動(壹);
finish();
timer . cancel();
}
},新日期(結束));
}
私有處理程序handler = new Handler(){
int num = 2;
public void handle message(Android . OS . message msg){
switch (msg.what) {
案例計數:
count down . settext(string . value of(num));
num-;
打破;
默認值:
打破;
}
};
};
}
count_down_background.xml
& lt?xml版本="1.0 "編碼="utf-8 "?& gt
& ltshape xmlns:Android = "/apk/RES/Android "
Android:shape = " rectangle " & gt;
& lt困境
Android:radius = " 8dp "/& gt;
& lt固體
Android:color = " # 1e 000000 "/>;
& lt/shape & gt;
方法二:
android封裝的類CountDownTimer。其實內部也是用Handler實現的。其他都壹樣。
包com . example . count timer;
導入Android . app . activity;
導入Android . content . intent;
導入Android . OS . bundle;
導入Android . OS . count down timer;
導入Android . view . window;
導入Android . widget . textview;
公開課WelcomeActivity拓展活動{
private final靜態int COUNT = 1;
私人TextView倒計時;
@覆蓋
受保護的void onCreate(Bundle saved instancestate){
super . oncreate(savedInstanceState);
requestWindowFeature(窗口。特征_編號_標題);
setContentView(r . layout . activity _ welcome);
init view();
}
私有void initView() {
count down =(TextView)findViewById(r . id . count _ down);//CountDownTimer構造函數的兩個參數是,第壹個參數代表總時間,第二個參數代表間隔時間。//表示每隔xxx就會回調onTick方法,然後在xxx之後回調onFinish方法。
CountDownTimer timer =新的CountDownTimer(3200,1000) {
int num = 2;
@覆蓋
公共void on tick(long millisuntil finished){
count down . settext(string . value of(num));
num-;
}
@覆蓋
Public void onFinish() {//定時完成調用
Intent I = new Intent(welcome activity . this,second activity . class);
i.addFlags(Intent。旗標_活動_清除_任務);
開始活動(壹);
finish();
}
};
timer . start();
}
}