Skip to content

Instantly share code, notes, and snippets.

@shinilms-nimo
Last active May 25, 2019 07:16
Show Gist options
  • Save shinilms-nimo/46159bc90dd53f4e49bcbc93bf8ea9d0 to your computer and use it in GitHub Desktop.
Save shinilms-nimo/46159bc90dd53f4e49bcbc93bf8ea9d0 to your computer and use it in GitHub Desktop.
Add app to whitelist of apps that are partially exempt from Doze and App Standby optimizations
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
//BE CAREFUL WITH THIS - App may get suspended from Google Play without prior notice for
//requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String packageName = getPackageName();
Intent intent = new Intent();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName)) {
//do stuff
} else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+packageName));
//OR
//Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS to see list of white listed apps
startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment