10

How can I loop that time() function on the onCreate every 5 seconds.. help me I'm a newbie in Android =) ... I want to execute time() function in onCreate every 5 seconds.

 public void onCreate(Bundle savedInstanceState) {
 time(); //<-- How can i execute this every 5 seconds.
 }

 private void time() {
new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
            int success;
         gps = new GPSTracker(AdminMenu.this);
        if(gps.canGetLocation()){
            tmplat=latitude;
            tmplong=longitude;  
           // new InsertUser1().execute();      
        }

        else{
        gps.showSettingsAlert();
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("LATTTTT" + tmplat);
        System.out.println("LONGGGGGGGG" + tmplong);
    } 

}, 5000); // 5 sec

 } 

Any help would be appreciated...

Roc Boronat
  • 11,395
  • 5
  • 47
  • 59
Chong
  • 115
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [Update the UI with dynamic text](http://stackoverflow.com/questions/9539416/update-the-ui-with-dynamic-text) – Knossos Jan 19 '16 at 15:22

5 Answers5

20

You could use a handler, given example would call yourfunction() every second

// Init
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        yourfunction();
        handler.postDelayed(this, 1000);
    }
};

//Start
handler.postDelayed(runnable, 1000);
10

Try to do like this ! just replace your code in startTimer method. It should work..

private Timer timer;
    private TimerTask timerTask;
    private Handler handler = new Handler();

    //To stop timer
   private void stopTimer(){
        if(timer != null){
            timer.cancel();
            timer.purge();
        }
    }

    //To start timer
    private void startTimer(){
        timer = new Timer();
        timerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run(){
                        //your code is here
                    }
                });
            }
        };
        timer.schedule(timerTask, 5000, 5000);
    }
Nik
  • 638
  • 2
  • 11
  • 24
  • thanks man.. its working.. but can u explain to me what is the use of (timerTask,5000,5000) i know that 5000 = 5 seconds.. but why is it two 5000? what is the use that? – Chong Jan 20 '16 at 03:27
  • it means schedule the timer, after the first 5000ms the TimerTask will run every 5000ms. If it's helpful to you don't forget to upvote this answer. – Nik Jan 20 '16 at 14:48
7

Here's a simple way to do it in Kotlin.

private lateinit var timer: Timer
private val noDelay = 0L
private val everyFiveSeconds = 5000L

override fun onResume() {
    super.onResume()

    val timerTask = object : TimerTask() {
        override fun run() {
            runOnUiThread { /* your code here */ }
        }
    }

    timer = Timer()
    timer.schedule(timerTask, noDelay, everyFiveSeconds)
}

override fun onPause() {
    super.onPause()

    timer.cancel()
    timer.purge()
}
Roc Boronat
  • 11,395
  • 5
  • 47
  • 59
6
 Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            if (activefragment == null)

            {
                fragmentTransaction.remove(activefragment);
            } else

            {
                fragmentTransaction.replace(R.id.fragmentContainer, activefragment, activefragment.getTag());
                fragmentTransaction.disallowAddToBackStack();
            }
            fragmentTransaction.commit();

        }
    };
    handler.postDelayed(runnable, timeout);
Behzad F94
  • 198
  • 2
  • 5
  • 6
    Although the code might help and work, please also add an explanation to your answer and how it solves the question's issue. – Capricorn Aug 20 '18 at 10:18
1

You can use a handler, given both example would call startLooping() every 5 second

Kotlin:

        //init
        val handler = Handler()
        val runnable: Runnable = Runnable {
            startLooping()
        }
    
        private fun startLooping() {
            handler.postDelayed(runnable, 5000)
        }

        //write inside onCreate method
        handler.postDelayed(runnable,5000)

Java:

        // Init
        private Handler handler = new Handler();
        private Runnable runnable = new Runnable()
        {
            @Override
            public void run() {
                // your work
                handler.postDelayed(this, 5000);    
            }
        };

        //write inside onCreate method
        handler.postDelayed(runnable, 5000);
Rahul
  • 3,293
  • 2
  • 31
  • 43