-6
如何在不冻结Android应用程序的情况下进行倒计时?例如倒数计数10到1。在Android上进行倒计时而不冻结应用程序?
如何在不冻结Android应用程序的情况下进行倒计时?例如倒数计数10到1。在Android上进行倒计时而不冻结应用程序?
从我的问题中可以得出,你很可能会遇到麻烦,因为你在主(UI)线程中进行某种倒计时。这肯定会导致应用在定时器期间冻结。只需使用一个定时器,做这样的事情:
//global variables
private int currTime = 10;
private TextView myTimer;//need to initialize this in your layout using findViewById, or programmatically
final Timer t = new Timer();
currTime = 10;
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {//You need this, or a Handler to ensure the UI is changed on the correct thread.
currTime--;
myTimer.setText(currTime);//myTimer is a global reference to your TextView.
if (currTime == 0)
t.cancel();
});
}
}, 10000);//note that this is the duration in milliseconds (1/1000 of a second) - so 10000 for 10 seconds.
与Java和Android是可能:-) –
看看它http://developer.android.com/reference/android/os/CountDownTimer .html –
我们需要看你的尝试 – Goaler444