简体   繁体   English

在打盹模式下启动前台服务

[英]Starting foreground service in doze mode

I'm developing an app which connects to a special device via wifi. 我正在开发一个通过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. FCM不是一种选择。 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. 因此无法使用JobScheduler 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. 但即使使用AlarmManager似乎也不是一个选项,因为在打瞌睡维护窗口期间我无法访问网络。

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() . 我想过使用一个WakefulBroadcastReceiver接收IntentAlarmManager ,需要WakeLock ,并把我的长时间运行的Service通过调用前景startForeground() But it seems that the startForeground() method has no effect on the Service as long as the device stays in doze mode. 但是,只要设备处于打盹模式, startForeground()方法似乎对Service没有影响。

I read many pages about doze and services but have no clue how to solve my problem... Does anyone got an idea? 我读了许多关于打瞌睡和服务的网页,但不知道如何解决我的问题...有没有人有个主意?

you should use GcmTaskService . 你应该使用GcmTaskService You can schedule some interval for your operations and it would work fine even in doze mode, check more information by link 您可以为您的操作安排一些间隔,即使在打盹模式下也能正常工作,请通过链接查看更多信息

You can use setAlarmClock, but it is not recommended for your purposes. 您可以使用setAlarmClock,但不建议用于您的目的。 Instead you can use setExactAndAllowWhileIdle with manual re-programming with an interval of 15 minutes. 相反,您可以使用setExactAndAllowWhileIdle进行手动重新编程,间隔为15分钟。 Best way: GCM. 最佳方式:GCM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM