Android 6 and 7 have some power optimizations (doze mode) which restrict app networking when device is not used.
User may disable optimization mode for any app in Battery settings:
Is it possible to check if optimization is enabled for my app or not? I need to ask user to disable optimization for better app functionality, but I don't know how to check it programatically.
As it turns out, we don't recommend battery optimization apps, as they do more harm than good. See our proven tips for better Android battery life for methods that actually work.
Add this permission in your manifest.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Request White-list / optimization enabled your app
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Intent intent = new Intent(); String packageName = getPackageName(); PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); if (!pm.isIgnoringBatteryOptimizations(packageName)) { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); startActivity(intent); } }
This one was a bit tricky to track down: here's what you are looking for
PowerManager.isIgnoringBatteryOptimizations()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With