sam_k
sam_k

Reputation: 6023

i want to call method every after every 5 minutes in background in android

hi friends in this task i have to call method after some time continueslly in background if i close the application than also call this method after define interval

Upvotes: 1

Views: 6053

Answers (3)

George
George

Reputation: 1327

use this

private final Handler _handler = new Handler();
private static int DATA_INTERVAL = 5 * 60 * 1000;

private final Runnable getData = new Runnable()
{
    @Override
    public void run()
    {
        getDataFrame();
    }
};
private void getDataFrame() 
{
    _handler.postDelayed(getData, DATA_INTERVAL);
}

getDataFrame() will call every 5 minutes until you will not kill him _handler.removeCallbacks(getData);

Upvotes: 1

Joel
Joel

Reputation: 3454

If you need anything ran after closing your application, it cannot be a thread that you started in your application, otherwise it would paradoxally mean that your application is not terminated yet.

You have to use a system service. The Android service you need to use is the AlarmManager. You'll find plenty of tutorials for it on Google.

Upvotes: 2

Moystard
Moystard

Reputation: 1837

It could be useful for your purpose: How to use a Handler to perform a task periodically How to use a Handler to perform a task periodically. It is more effective than using a traditional TimerTask. You can do this in a Service so if you close the application, it will still occur.

Upvotes: 2

Related Questions