Android的线程的计时器 [英] Android Thread for a timer

查看:129
本文介绍了Android的线程的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MainActivity extends Activity
{
int min, sec;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    min = 5;
    sec = 0;
    final TextView timer1 = (TextView) findViewById(R.id.timer1);
    timer1.setText(min + ":" + sec);
    Thread t = new Thread() {
        public void run() {
                sec-=1;
                if (sec<0) {
                    min-=1;
                    sec=59;
                }
                timer1.setText(min + ":" + sec);
                try
                {
                    sleep(1000);
                }
                catch (InterruptedException e)
                {}
            }
    };
    t.start();
}
}

这是一个code在Java的线程,但它不工作。你能帮助我吗?

This is a code for a Thread in Java but it doesn't work. Can you help me?

它是一个定时器计数从5分钟缩短到晚上12点。

Its a Timer that counts down from 5 Minutes to 0:00.

推荐答案

在您使用线程的情况下。所以你不能更新从线程比UI线程其他UI。所以你用 runOnUithread 。我建议你​​使用一个倒数计时器或处理程序。

In your case you are using threads. So you cannot update ui from the thread other than the ui thread. SO you use runOnUithread. I would suggest you to use a countdown timer or a Handler.

1.CountDownTimer

1.CountDownTimer

http://developer.android.com/reference/android/os/ CountDownTimer.html

下面有一个链接到另一个例子。建议你检查链接的倒数计时器。

Here's a link to another example. Suggest you to check the link for the count down timer.

<一个href="http://stackoverflow.com/questions/17620641/countdowntimer-in-minutes-and-seconds/17620827#17620827">Countdowntimer以分和秒

例如:

 public class MainActivity extends Activity {

Button b; 
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
    b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startTimer(200000); 
        }

    });
}
private void startTimer(long time){
    CountDownTimer counter = new CountDownTimer(30000, 1000){
        public void onTick(long millisUntilDone){

           Log.d("counter_label", "Counter text should be changed");
          tv.setText("You have " + millisUntilDone + "ms");                    
        }

        public void onFinish() {
            tv.setText("DONE!");

        }
    }.start();
}
 }

2.您可以使用处理程序

例如:

Handler m_handler;
Runnable m_handlerTask ; 
int timeleft=100;
m_handler = new Handler(); 
m_handlerTask = new Runnable() 
{ 
@Override
public void run() {
if(timeleft>=0)
{  
     // do stuff
     Log.i("timeleft",""+timeleft);
     timeleft--; 
}      
else
{
  m_handler.removeCallbacks(m_handlerTask); // cancel run
} 
  m_handler.postDelayed(m_handlerTask, 1000); 
 }
 };
 m_handlerTask.run();     

3.每

定时器在不同的线程中运行。您应该更新用户界面UI线程。使用 runOnUiThread

Timer runs on a different thread. You should update ui on the ui thread. use runOnUiThread

例如:

  int timeleft=100;
  Timer _t = new Timer();  
  _t.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {

               runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                    Log.i("timeleft",""+timeleft);  
                    //update ui

                  }
                 });
                 if(timeleft>==0)
                 { 
                 timeleft--; 
                 } 
                 else
                 {
                 _t.cancel();
                 }
            }
        }, 1000, 1000 ); 

这篇关于Android的线程的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆