0

I have written a service that fetches data from a CRM based web service and puts in a database on the phone. Now this service has to be run every 3 hours, so it can sync data between CRM and the android database.

Now to have this service run itself, I'm using alarm manager and have the web service "start" itself.

Intent intent = new Intent(ServiceClass.this, ServiceClass.class);
PendingIntent pintent = PendingIntent.getService(ServiceClass.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10800*1000, pintent);

This service needs to be started after a reboot, for that purpose I'm using the method outlined here..

I just want to know if I'm going on the right path, or if I'm making a mistake or if there is a better way to do this. I haven't worked with Android much and just need a few pointers. Thanks!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64

1 Answers1

2

Yes you are on the right path. The Alarm Manager is very reliable and is used specifically for this purpose - to schedule tasks in the future (both repeating and non-repeating).

And I agree with @tyczj, you should definitely define your ServiceClass as an IntentService. An IntentService is a subclass of Service which runs in the background and is specifically designed to perform a specific task, and then kill itself once it's finished. It's perfect for downloading data and background syncing.

It's quite easy to implement as well, you'll most likely just need to override one method - onHandleIntent - which is what is called when the service starts.

To re-schedule the tasks on reboot, the method outlined in that post is what I use. Hope this helps!

physphil
  • 233
  • 2
  • 9