是否可以使用Toasts或ProgressDialog制作倒数计时器?
当我启动我的应用程序时,弹出一个带有文本加载的ProgressDialog ...直到加载数据,我关闭对话框和listview弹出文本视图和内容。
但是在启动时我检查我的网络状态......当网络连接时,我开始我的后台课程..但如果不是,我会用计时器每10秒重启一次活动(如下所示) )。但是如果没有连接,我会在烤面包上显示一个计数器或更改我的对话框中的文字,例如:(show - >)" 10后自动刷新..." (< - hide)(show - >)""自动刷新9 ..."直到" 0后自动刷新..."然后10秒过去..和完成()线;和startActivity开始..
这完全在我的onCreate中:
global.loading_dialog(this); //start progressdialog
boolean network_connected = false;
if(check_network.isInternetAvailable(this)) {
network_connected = true;
new connect_task_main().execute(""); //the background class starts
} else {
network_connected = false;
global.toast.setText("No Internet Connection"); //toast text
global.toast.show(); //toast start
global.cancel_toast(2500); //toast stop
}
if (network_connected == false) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
global.spinning_dialog.dismiss(); // when the task activates, then close the dialog
timer.cancel(); //stop de timer
finish(); //close the activity
startActivity(new Intent(main_activity.this, main_activity.class)); //start a new activity
}
}, 10000); //after 10 seconds, the task will be active.
} else {
}
答案 0 :(得分:1)
Android确实有现成的CountDownTimer
答案 1 :(得分:0)
您可以使用Handler
每秒呼叫一次,当它被调用10次时,您可以更改Activity
。
做类似的事情:
final int i = 0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Display the time like : textView.setText("Automatic refresh after " + 10 - i +"...");
if (i == 10)
{
global.spinning_dialog.dismiss(); // when the task activates, then close the dialog
finish(); //close the activity
startActivity(new Intent(main_activity.this, main_activity.class)); //start a new activity
}
else
{
i++;
handler.postDelayed(this, 1000); // Call it 1 second later
}
}
};
handler.postDelayed(runnable, 0); // Call it immediatly
答案 2 :(得分:0)
感谢@ giant00的回答: CountDownCounter
我更改了部分代码(如下所示):
if (network_connected == false) {
new CountDownTimer(12000, 1000) {
public void onTick(long millisUntilFinished) {
global.toast.setText("Automatic Refresh In: " + millisUntilFinished / 1000);
global.toast.show();
}
public void onFinish() {
finish();
startActivity(new Intent(main_activity.this, main_activity.class));
}
}.start();
} else {
}