Thursday, 7 June 2012

Create Thread for do work in every 2 minute in Android

You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event.





private final static int INTERVAL = 1000 * 60 * 2; //2 minutes
Handle m_handler;


Runnable m_handlerTask = new Runnable()
{
     @Override 
     public void run() {
          doSomething();
          m_handler.postDelayed(m_handlerTask, INTERVAL);
     }
}


void startRepeatingTask()
{
    m_handlerTask.run(); 
}


void stopRepeatingTask()
{
    m_handler.removeCallback(m_handlerTask);
}

No comments:

Post a Comment