Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of setTimeOut() javascript to Android?

Tags:

I need the equivalent code of setTimeOut(call function(),milliseconds); for android.

setTimeOut(call function(),milliseconds); 
like image 383
José Francisco Lara Aguilar Avatar asked Jan 07 '12 18:01

José Francisco Lara Aguilar


People also ask

What is setTimeout function in JavaScript?

setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

Which is better setTimeout or setInterval?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

What is setInterval in Java?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .


2 Answers

You probably want to check out TimerTask

Since you brought up this again I'd like to make a different recommendation, which is a Handler. It is simpler to use than a TimerTask as you won't need to explicitely call runOnUiThread as the Handler will be associated with the UI thread so long as it's created on the UI thread or you create it using the main looper in it's constructor. It would work like this:

private Handler mHandler; Runnable myTask = new Runnable() {   @Override   public void run() {      //do work      mHandler.postDelayed(this, 1000);   } }  @Override public void onCreate(Bundle savedState) {   super.onCreate(savedState);   mHandler = new Handler(Looper.getMainLooper()); } //just as an example, we'll start the task when the activity is started @Override public void onStart() {    super.onStart();   mHandler.postDelayed(myTask, 1000); }  //at some point in your program you will probably want the handler to stop (in onStop is a good place) @Override public void onStop() {   super.onStop();   mHandler.removeCallbacks(myTask); } 

There are some things to be aware of with handlers in your activity:

  1. Your activity can be shutdown/non visible while your handler is still running if you don't stop it in onStop (or onPause if you started it in onResume), this will lead to problems if you try to update the UI
  2. If your phone goes into deep sleep the handlers won't fire as often as you have specified. I know this as I've done some extensive testing with bluetooth devices to test connectivity after many hours of operation and I had used handlers along with log prints every time they fired.
  3. If you need this timer to be ongoing I recommend putting it in a service which will last longer than an activity. Register your activity with the service(by implementing an interface defined in the service for communication with the service).
like image 116
Matt Wolfe Avatar answered Sep 22 '22 01:09

Matt Wolfe


This is the code that i used in my current project. I used TimerTask as Matt said. 60000 is the milisec. = 60 sec. i used it to refresh match scores.

private void refreshTimer() {         autoUpdate = new Timer();         autoUpdate.schedule(new TimerTask() {             @Override             public void run() {                 runOnUiThread(new Runnable() {                     public void run() {                         adapter = Score.getScoreListAdapter(getApplicationContext());                         adapter.forceReload();                         setListAdapter(adapter);                     }                 });             }         }, 0, 60000); 
like image 36
GiantRobot Avatar answered Sep 22 '22 01:09

GiantRobot