klemens
klemens

Reputation: 11

Android - doing background work every few minutes

I need to write some application which will be do some background work for every few minutes. My question is how I can start this work from service. Do I need make this using Threads and calculating time using some System utils or maybe there is better solution?

Upvotes: 1

Views: 244

Answers (3)

estoy
estoy

Reputation: 122

(cant seem to add comment, so adding as an answer)

since this task will be performed continuosly, i'd extend Xion's example like this:

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable()
  public void run() {
     // your work
     //...
     handler.postDelayed(this, minutes * 60 * 1000); //this will schedule the task again
  }
}, minutes * 60 * 1000);

Upvotes: 1

Xion
Xion

Reputation: 22780

You can use Handler and postDelayed method:

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable()
    public void run() {
       // your work
    }
}, minutes * 60 * 1000);

If the interval is sufficiently long you can also consider using AlarmManager.

Upvotes: 1

user824330
user824330

Reputation: 304

No you don't need to make by using threads. You can simply do this by AlarmManager. For the reference see this link it will helps you. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html

Upvotes: 0

Related Questions