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

前端 未结 5 620
天命终不由人
天命终不由人 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:04

    Finally my problem solved. Thanks to @Stevensen, @Xavier and one of my Friend who helps me to identify the problem. It is related to doze mode.


    Some mobiles manufactures(Xiomi, Huawei etc) implemented SmartManager to optimize battery consumption. They have a kind of battery manager that kill apps, and when an app is killed, scheduled alarms are canceled and also they did not detect any active network or blocks network call from background service. Because manufactures blames non trusted apps for power consumption. Facebook, Whats App, etc apps are trusted and they are whitelisted by manufactures. That's why they are able to call network event even if app killed.


    Still I did not find any solution for that.So temporary I overcome this problem for Xiomi devices. I keep out my app from battery saving restriction than its working correctly by doing following things:

    settings--> battery -> Power --> App battery saver --> your app 
    Now select No restrictions( for Background settings) then Allow option for Background location 
    


    For android M version and above that, app have to ask permission :

    Intent intent = new Intent();
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);
    

    and in manifest :

    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
    

    After that user can do whitelist your app.

    0 讨论(0)
  • 2021-02-05 13:04

    I had the same problem: "java.net.UnknownHostException: Unable to resolve host "host_name": No address associated with hostname". Also Internet was available with all granted Android permissions (tested them all even at runtime).

    But the solution was different. The reason was that our API-host "host_name" (e.g. http://xxx.yyyy.zz) was in LAN and was not able from outer network. The same issue may be caught if you are calling your company's outer "host_name" from company's LAN (it seems like "DNS Rebind attack" for server). To test it you should try to open used url in a browser when the device is (and when isn't) connected to Internet from a local LAN (e.g. company's Wi-Fi) and check if server response is correct.

    The @Mangesh Sambare's problem was solved as he said above, but maybe this experience'll be helpful for somebody who're in the same situation as I was.

    0 讨论(0)
  • 2021-02-05 13:19

    When the app goes into background mode, you need to wake up the app every now and then to sniff the mobile phone’s position. Naturally, the more often it would sniff, the faster and more reliably it would be able to detect the geofence. https://proximi.io/will-my-geofencing-function-in-the-background

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 13:29

    Maybe your app gets blocked to use network by Androids doze mode and/or app standby. Check Optimizing for Doze and App Standby.

    A possible solution is to setup an alarm with the AlarmManager. Android will schedule the alarms processing in a maintenance window where you are allowed to use network.

    0 讨论(0)
提交回复
热议问题