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
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 :
After that user can do whitelist your app.