PowerManager.isIgnoringBatteryOptimizations always returns true even if is removed from 'Not optimized apps'
Asked Answered
A

2

12

I have a mileage logbook app which does GPS tracking and is able to establish a OBDII connection to a car in background.

Now I want to show a Popup which informs the users if my app is not whitelisted in doze since this may stop my background (actually foreground) services...

I do:

 String PACKAGE_NAME = getApplicationContext().getPackageName();
            PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
            boolean status = false;
            status = pm.isIgnoringBatteryOptimizations(PACKAGE_NAME);

            if (!status) {
                // show popup
            }

but PowerManager.isIgnoringBatteryOptimizations always returns 'true' even if it is removed from 'Not optimized apps' again. Only if I uninstall the app 'false' is returned again... Tested on Galaxy Note 8 (Android 8.0) and Emulator 8.1

Question is simple: Is this a bug? Or how to remove the app from whitelist so that PowerManager.isIgnoringBatteryOptimizations is returnung 'false' again?

Amblyopia answered 8/5, 2018 at 10:55 Comment(6)
I don't see the behavior you describe with 8.0 or 8.1 emulators. I do see this related issue with refresh of the "Not optimized" app list: issuetracker.google.com/issues/37067894Ballman
I guess your app must satisfy these whitelisting conditions, only then would it be whitelisted from any optimisation.Kudos
@Rahul Thakur: -> Task automation app -> App's core function is scheduling automated actions, such as for instant messaging, voice calling, new photo management, or location actions. If applicable. AcceptableAmblyopia
I guess your solution lies in starting a foreground service when required instead of trying to get the app in the whitelist to avoid battery optimisations. Check out this [link] (developer.android.com/training/monitoring-device-state/…) to understand more about foreground services. I would also recommend you to check out the 2016 Google IO session about app doze and standby to figure out what works best for you. This will help you figure out a user interaction which does not involves whitelisting your app.Kudos
@RahulThakur the service still gets killed in custom UI like MIUI/funtouch OS/oxygen OS. any solution to this problem?Lobell
I am also facing the same issue.Boric
D
3

I have made simple one activity app that requests

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

and put this code into onCreate

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent();
    String packageName = this.getPackageName();
    PowerManager pm = (PowerManager) this.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));
    }
    this.startActivity(intent);
}

I have tested it on 8.1 emulator and I got the same behavior. You can possibly unisntall app or switch its state from optimized to not optimized in all app list in settings. I guess Bob Snyder showed right issue in google tracker.

Delafuente answered 29/5, 2020 at 14:46 Comment(0)
B
1

Same here. Android 9 emulator. Uninstalling the app and installing it again makes the method to work again.

Byron answered 12/8, 2022 at 18:50 Comment(1)
ok,where is the reference ?Bondmaid

© 2022 - 2024 — McMap. All rights reserved.