11

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?

stefan
  • 1,336
  • 3
  • 21
  • 46
  • 1
    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: https://issuetracker.google.com/issues/37067894 – Bob Snyder Jun 27 '18 at 19:42
  • I guess your app must satisfy these [whitelisting conditions](https://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-cases), only then would it be whitelisted from any optimisation. – Rahul Thakur Oct 30 '18 at 10:03
  • @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. Acceptable – stefan Nov 04 '18 at 09:12
  • 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] (https://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-cases) 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. – Rahul Thakur Nov 13 '18 at 07:14
  • 1
    @RahulThakur the service still gets killed in custom UI like MIUI/funtouch OS/oxygen OS. any solution to this problem? – Parth Anjaria Feb 26 '19 at 18:39
  • I am also facing the same issue. – Mohammed mansoor May 04 '20 at 08:09

2 Answers2

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.

thekekc
  • 206
  • 2
  • 12
1

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

Mister Smith
  • 27,417
  • 21
  • 110
  • 193