user1492913
user1492913

Reputation: 21

How to schedule long running network tasks at specfic time in android?

I have to schedule some long running network task(downloading files) at some specific time (for ex during night hours). Now it can be done using job scheduler but in doc it says that it should be used for shorter tasks(less then 3 mins), another option is using service with alarm manager but that is discouraged. So what to choose between these two or is there any other approach also.

Upvotes: 2

Views: 111

Answers (1)

Nick Friskel
Nick Friskel

Reputation: 2437

Your best bet is to use an AlarmManager to schedule an IntentService to be ran at your specific time and interval.

You will want to have the alarm fire a WakefulBroadcastReceiver which then starts your service, because it sounds like your network task will be ran at night when the phone is asleep. This will ensure that the service gets ran right away, just be sure the wake lock is released when the tasks are done!

To release the wakelock, have this at the end of your service:

MyWakefulReceiver.completeWakefulIntent(intent);

Where MyWakefulReceiver is the receiver you made which extends WakefulBroadcastReceiver. If you need any more help setting this up let me know.

Upvotes: 1

Related Questions