Can I use Android Beacon Library in foreground service, even on Android 8 (Oreo)?

半世苍凉 提交于 2019-12-03 16:41:34

This approach is sound. The Android Beacon Library 2.15+ natively supports foreground services as a scan mechanism for supporting cases like this on Android 8. See here for more info.

The tricky part is to switch back and forth between using Job Scheduler and a Service to do scanning. I have not tested this, but my suggestion would be to bind manually to the BeaconManager in a custom Application class. Then :

  1. On entering a region, stop monitoring and then unbind the BeaconManager.

  2. Start a custom foreground service

  3. In the foreground service, disable Scan Jobs, then bind to the BeaconManager and start ranging

  4. Once no beacons have been ranged for a time, stop ranging, unbind from the BeaconManager, enable Scan Jobs, bind again and then start monitoring.

  5. Finally, exit the foreground service

On the second question, yes, START_STICKY will very quickly restart a service on most platforms. The library uses a 5 minute timer with the AlarmManager as a backup, which will relaunch the service if a START_STICKY restart fails. Indeed, in typical use, the scanning service restarts much more quickly than five minutes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!