I'm developing an Android app that uses Xiaomi Mi Band 1S to keep measurements of heart rate all the time. I use a service to handle Bluetooth connection and I already achieved to keep this service alive when the app is close, and even to restart the service when the mobile is rebooted.
Now, my problem comes with android doze mode. I'm using the following tricks to keep the service alive:
- Use of wakelocks
- Use of wakefullBroadcastReceiver that restarts the service when closed with the following filters:
"android.intent.action.BOOT_COMPLETED"
"android.intent.action.QUICKBOOT_POWERON"
- My own intent called when the app is closed and by an alarm every 10 minutes.
- Programmatically requiring to ignore battery optimizations.
CODE
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
if (powerManager != null && !powerManager.isIgnoringBatteryOptimizations(packageName)){
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
PROBLEM
My mobile phone is a Xiaomi. Both, BOOT_COMPLETED
and IGNORE_BATTERY_OPTIMIZATIONS
, aren't working. They only work if I set the permissions manually. I also added the required permissions to the manifest file.
So is there a way to allow these permissions without the user having to set them manually? Applications like WhatsApp or Skype have these permissions by default. Why I can't do the same?
Also, this is happening on Xiaomi mobile. It will also happen in all other mobile devices?