3

I'm developing an app which connects to a special device via wifi. I need to make status updates in a short interval, resp. keep the status of my special device in the app up to date. FCM is not an option. My idea is to give the user two options: Fast updates using a foreground service or "slow" updates using a periodical update mechanism to save battery.

My question is about the second option. The interval should be around five minutes. Using JobScheduler therefore is not possible. But even using the AlarmManager seems not to be an option because I'm not able to get network access during the doze maintenance windows.

I thought about using a WakefulBroadcastReceiver to receive the Intent by the AlarmManager, require a WakeLock and turn my long running Service into foreground by calling startForeground(). But it seems that the startForeground() method has no effect on the Service as long as the device stays in doze mode.

I read many pages about doze and services but have no clue how to solve my problem... Does anyone got an idea?

bartolja
  • 487
  • 1
  • 4
  • 13
  • The only thing that can really wake the device from doze is a FCM message, other than that I dont think you really have any options to do what you want unless you want to tell all your users to exclude your app from doze mode – tyczj Mar 15 '17 at 17:02

2 Answers2

2

you should use GcmTaskService. You can schedule some interval for your operations and it would work fine even in doze mode, check more information by link

A. Shevchuk
  • 2,109
  • 14
  • 23
  • Thanks! Haven't seen this before. Nice solution! – bartolja Mar 16 '17 at 08:16
  • 1
    For others interested in this: [Here](http://blog.fossasia.org/working-with-gcm-task-service-on-android/) is a great tutorial – bartolja Mar 16 '17 at 08:19
  • By using GcmTaskService,Can I make my app should always connect with internet to make sure app won't lost notifications? – kavie May 31 '18 at 05:37
  • @kavie no, gcm will execute only in special periods of time. In order to receive notifications you may use [push notifications](https://firebase.google.com/docs/cloud-messaging/android/client) – A. Shevchuk May 31 '18 at 07:00
  • I'm using third party server to receive notification in my android wear app.Is there way way to make my app, not to lost notification when android devices enters in to doze mode? (* I can't go with FCM now) – kavie May 31 '18 at 07:08
0

You can use setAlarmClock, but it is not recommended for your purposes. Instead you can use setExactAndAllowWhileIdle with manual re-programming with an interval of 15 minutes. Best way: GCM.

iClaude
  • 163
  • 1
  • 9