Geofencing : HTTP request failed while sending through the background service. Gives UnknownHostException

前端 未结 5 619
天命终不由人
天命终不由人 2021-02-05 12:48

I implemented Geofence in android application. I followed this link to implement \'Geofence\' in app. I am using \'Retrofit\' library to call \'HTTP\' request.


Ap

5条回答
  •  失恋的感觉
    2021-02-05 13:22

    The explanation by @Stevensen about Doze mode being the reason of the failure is more likely the cause. In the documentation you can read:

    The following restrictions apply to your apps while in Doze: Network access is suspended...

    I'll suggest to store the events in a DB, and schedule a job to upload them to the server by using JobScheduler (API 21+, see a tutorial here) or, if you need to support older devices, by using this replacement firebase-jobdispatcher (which provides a JobScheduler-compatible API by wrapping GCM Network Manager).

    I'll suggest to set a condition of network is needed: .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) and probably a .setPeriodic(long intervalMillis) to limit the number of times it happens (for example, upload at most once per hour).

    As long as no realtime is required, it's a better approach for the user experience to save battery: Doze mode will help the device to save battery life, and JobScheduler will allow to batch uploads and just wake up the radio from time to time, saving battery life. See this quick video for the rationale.

提交回复
热议问题