12

On Xiaomi's MI devices, there is a feature of turning off/on "Autostart" in their security app. (In Security App->Permissions->AutoStart)

enter image description here

This means none of the broadcast receivers receive anything while the app is not running. So BOOT_COMPLETED, USER_PRESENT, CONNECTIVITY_CHANGE, etc... do not work. (They work for a while after the app is on foreground, but stop soon). They also stop working after the user swipes the app from Xiaomi's version of "recent apps"

Even GCM fails to wake it up

For messaging apps, this is a killer.

By default, apps like Whatsapp, Messenger, Flipkart etc... are enabled by default (Even if these apps are not pre-installed).

Most other apps have this disabled by default. eg. Slack is disabled by default.

Is there a way to get on this white-list by default?

Gautam
  • 475
  • 1
  • 3
  • 8

1 Answers1

0

You can actually disable the battery optiomisation for your app. It'll turn all off the optimisations for the app so it won't get killed.

boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
if(!isIgnoringBatteryOptimizations){
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, MY_IGNORE_OPTIMIZATION_REQUEST);
}

Now check if the optimisation has been disabled for your app.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_IGNORE_OPTIMIZATION_REQUEST) {
        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
        if(isIgnoringBatteryOptimizations){
            // Ignoring battery optimization
        }else{
           // Not ignoring battery optimization
        }
    }
}
Dev4Life
  • 2,328
  • 8
  • 22