Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Run a thread repeatingly within a timer

First of all, I could not even chose the method to use, i'm reading for hours now and someone says use 'Handlers', someone says use 'Timer'. Here's what I try to achieve:

At preferences, theres a setting(checkbox) which to enable / disable the repeating job. As that checkbox is checked, the timer should start to work and the thread should be executed every x seconds. As checkbox is unchecked, timer should stop.

Here's my code:

Checking whether if checkbox is checked or not, if checked 'refreshAllServers' void will be executed which does the job with timer.

boolean CheckboxPreference = prefs.getBoolean("checkboxPref", true);
                if(CheckboxPreference == true) {
                    Main main = new Main();
                    main.refreshAllServers("start");
                } else {
                    Main main = new Main();
                    main.refreshAllServers("stop");
                }

The refreshAllServers void that does the timer job:

public void refreshAllServers(String start) {

    if(start == "start") {

        // Start the timer which will repeatingly execute the thread

    } else {

        // stop the timer

            }

And here's how I execute my thread: (Works well without timer)

Thread myThread = new MyThread(-5);
                myThread.start();

What I tried?

I tried any example I could see from Google (handlers, timer) none of them worked, I managed to start the timer once but stoping it did not work. The simpliest & understandable code I saw in my research was this:

new java.util.Timer().schedule( 
        new java.util.TimerTask() {
            @Override
            public void run() {
                // your code here
            }
        }, 
        5000 
);
like image 622
sarkolata Avatar asked Jul 24 '12 21:07

sarkolata


People also ask

How do you call every 10 seconds on Android?

This example demonstrates how do I run a method every 10 seconds in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can we call run method twice in thread?

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

Will thread automatically be killed?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads.


1 Answers

Just simply use below snippet

private final Handler handler = new Handler();
private Runnable runnable = new Runnable() {
    public void run() {
         //
         // Do the stuff
         //

         handler.postDelayed(this, 1000);
    }
};
runnable.run();

To stop it use

handler.removeCallbacks(runnable);

Should do the trick.

like image 104
Tuna Karakasoglu Avatar answered Nov 06 '22 08:11

Tuna Karakasoglu