0

I am working on an Android app which runs a periodic service (every 15 minutes) using AlarmManager.

The service sends/receives data over the internet.

I have noticed when 3G is enabled and the phone is idle. It has difficulty communicating with the internet. Its as if 3G doesnt automatically turn on when it needs to.

The service works great when the user is using the phone (and 3G) at the same time but not when the phone is idle.

Any suggestions how to trigger 3G when I need to?

Abhishek
  • 1,749
  • 9
  • 27
  • 39

1 Answers1

0

You need to obtain and hold a WakeLock when the phone is not in use. Otherwise the phone will go to sleep and not process your request.

It's important to create the wakelock in your BroadcastReceiver, the one that is called from the AlarmManager. Then set the WakeLock to a static reference and invoke the service. The service will then unset the static reference and continue to hold the WakeLock until the code is finished.

Just a tip, you might want to look into AlarmManager.setInexactRepeating to save power. It groups similar alarms together so the phone wakes up less times. Also a 15 minute interval is very short. You'll consume a lot of battery by just waking up the phone with that timespan. An hour minimum is good though.

Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99