I'm new in android
. I struggle with my application approximately 3 weeks. I need sent and receive packets in normal mode and sleep mode
. My app
must exchange data a 5 seconds. I tried using alarmmanager
but on android 5 it's not works. On android
5 an interval changes it on 60 seconds. Such a solution makes the battery wears out quickly. When I use normal asynctask, not IntentService
, then it works only when screen is ON
and app
is visible. When app is hidden or I click power OFF
then exchange data stops working. What is the best solutions?
-
1You must use a Service for this. and yes this will quickly drain the battery – Nanoc Nov 25 '15 at 15:00
-
1AlarmManager is the way to go. You can customize it to whatever interval you want. It doesn't have to be 60 seconds... – NoChinDeluxe Nov 25 '15 at 15:03
-
When I use a Service then the application works only when the screen is turned on and the application is visible. I use also alarmmanager with IntentService but on android 5 a interval changes on 60 seconds. – SeaDog Nov 25 '15 at 15:05
-
@SeaDog - See my answer below. – NoChinDeluxe Nov 25 '15 at 15:15
-
@SeaDog, could you manage to implement it ? – cgr Nov 25 '15 at 19:46
-
@SeaDog can you delete your answer as it is not actually an answer. – cgr Nov 26 '15 at 16:00
-
@cgr, I removed my answer. – SeaDog Nov 27 '15 at 16:04
2 Answers
Even RTC_WAKEUP doesn't help most of the times.
Solution that worked for my app when device in deep sleep mode:
Use WakefulBroadcastReceiver combined with AlarmManager.
Service is started by startWakefulService() and when it is finished, it releases the wake lock by calling completeWakefulIntent(intent). So the device will be allowed to go back to sleep.
I'm not adding any code. Search for examples on how to use WakefulBroadcastReceiver with AlarmManager. Even WakefulBroadcastReceiver doc has some template code.
Also reduce the frequency of alarm so you can avoid draining so much battery.

- 4,578
- 2
- 28
- 52
-
Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/96317/discussion-on-answer-by-cgr-how-to-use-http-in-sleep-mode). – elixenide Nov 27 '15 at 05:09
-
1
You can use the AlarmManager
class to wake up the device at a particular time, then fire off an operation at whatever interval you'd like. Code from the docs found here:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
Notice the last line of this block. You can use the method setRepeating()
to set whatever interval you'd like.

- 3,446
- 1
- 16
- 29
-
-
@drschultz, Yes. We have used very recently in our app and it works even in deep sleep mode. We don't have the latest version yet in play store but will be available soon so you can verify it for yourself. – cgr Nov 26 '15 at 13:15