简体   繁体   中英

Android Timer Tasks

I'm trying to make a timer that will do a certain thing after a certain amount of time:

 int delay = 1000; 

        int period = 1000; 

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

            //Does stuff

        }
        }, delay, period);

However, the app crashes after the wait period. This is Java code, so it might not be entirely compatible with Android (like while loops). Is there something I'm doing wrong?

Something like this should work, create a handler, and wait 1 second :) This is generally the best way of doing it, its the most tidy and also probably the best on memory too as its not really doing too much, plus as it's only doing it once it is the most simple solution.

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
   public void run() { 
   // do your stuff
   } 
}, 1000); 

If you would like something to run every one second then something like this would be best:

Thread thread = new Thread()
{
    @Override
    public void run() {
        try {
            while(true) {
                sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
thread.start();

If you want a GUI thread then something like this should work:

    ActivityName.this.runOnUiThread(new Runnable()
    {
     public void run(){
 try {
            while(true) {
                sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     }
     });

If your app is crashing after the wait period, then your timer task is doing its job and executing your code on schedule. The problem must then be in your code where run() occurs (for example, you may be trying to update UI elements in a background thread).

If you post more code and your logcat, I can probably be more specific about the error you are getting, but your question was in regards to TimerTask.

Timer and also you can run your code on UI thread:

public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);
     myTimer = new Timer();
     myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            timerMethod();
        }
    }, 0, 1000);
}

private void timerMethod(){
    //  This method is called directly by the timer
    //  and runs in the same thread as the timer.
    //  We call the method that will work with the UI
    //  through the runOnUiThread method.
    this.runOnUiThread(timerTick);
}

private Runnable timerTick = new Runnable() {

    public void run() {
        //  This method runs in the same thread as the UI.               
        //  Do something to the UI thread here
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM