0

我想显示计时器,每一秒。20 秒后,我希望该活动自行调用。

但是,当我不显示计时器时,它会按我的意愿等待 20 秒,但是一旦我实现代码来显示计时器,它就会启动并突然停止。

这是我的代码。请帮帮我。

public class MainActivity extends Activity { 
public int time=20;
Button end;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Thread timerdisp = new Thread(){
        TextView tv = (TextView) findViewById(R.id.timer);

        public void run(){
            try{    
                sleep(1000); // sleep for 1 seconds
                tv.setText(String.valueOf(time));
                time-=1;
                if(time==0){
                    startActivity(new Intent(MainActivity.this,MainActivity.class));                        
                }
                run();
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }; 
    timerdisp.start();
    );
}
4

4 回答 4

2

Android 提供了更好的CountDownTimer工具,也许你应该使用它。因为它提供了许多内置方法并默认在后台线程上运行。

您可以使用onFinish()方法来执行对活动的调用。

这是一个相同的例子

于 2013-05-22T07:22:48.817 回答
2

试试下面的代码:

private long ms=0;
    private long splashTime=2000;
    private boolean splashActive = true;
    private boolean paused=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hides the titlebar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.splash);

        Thread mythread = new Thread() {
            public void run() {
                try {
                    while (splashActive && ms < splashTime) {
                        if(!paused)
                            ms=ms+100;
                        sleep(100);
                    }
                } catch(Exception e) {}
                finally {
                    Intent intent = new Intent(Splash.this, Home.class);
                    startActivity(intent);
                }
            }
        };
        mythread.start();
    }

你也可以试试这个

public class MainActivity extends Activity {

    private CountDownTimer countDownTimer;
    public TextView text;
    private final long startTime = 20 * 1000;
    private final long interval = 1 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.textView1);

        countDownTimer = new MyCountDownTimer(startTime, interval);
        text.setText(String.valueOf(startTime / 1000));
        countDownTimer.start();

    }

    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(),xxxx.class);
            startActivity(intent);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            text.setText("" + millisUntilFinished / 1000);
        }
    }

}
于 2013-05-22T08:04:25.077 回答
1

利用runOnUiThread

这是正常的Thread,不会让您的 UI 进入休眠状态。并且系统不会挂起。

或者您也可以使用AsyncTask. 我会更喜欢你使用AsyncTask.

于 2013-05-22T07:31:56.220 回答
-1

使用下面的代码每 20 秒调用一次函数。

private Timer timer;
TimerTask refresher;                 

timer = new Timer();    
refresher = new TimerTask() {
  public void run() {
                            // your code to call every 20 seconds. 
                     };
                 };
// first event immediately,  following after 20 seconds each
timer.scheduleAtFixedRate(refresher, 0,1000*20); 

使用下面的行来显示时间:

package com.example.test;
public class MainActivity extends Activity {

private Long startTime;
public native String getLastShotName();
public native String colorNormal();
public native String flipImage();
public native String forceInvertColor();
public native String getLastTitle();
public native String myMethod();
boolean mbFlip = false;
private Timer timer;
private Handler handler = new Handler();  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);           
          TimerTask refresher;
             startTime = System.currentTimeMillis();
             handler.removeCallbacks(updateTimer);
             handler.postDelayed(updateTimer, 1000);
    }
    @Override
    protected void onResume() {
        super.onResume();
         handler.postDelayed(updateTimer, 1000);
    }         
    private Runnable updateTimer = new Runnable() {
                  public void run() {
                      final TextView time = (TextView) findViewById(R.id.textView1);
                      Long spentTime = System.currentTimeMillis() - startTime;                  
                      Long minius = (spentTime/1000)/60;                          
                      Long seconds = (spentTime/1000) % 60;
                      time.setText(minius+":"+seconds);
                      handler.postDelayed(this, 1000);
                  }
              };
}         
于 2013-05-22T07:17:00.950 回答