34

I have IntentService task in foreground mode, but in Android M+ the task stops in Doze mode. I read Google banned if the app uses intent to set themselves in whitelist. But if I use permission and check GRANT or DENIED, I get the granted result, but nothing happen. I don't see my app in whitelist. How can I add the app in whitelist without banned? (I added permission in AndroidManifest.xml)

if(Build.VERSION.SDK_INT>=23){
    int permissionCheck= ContextCompat
                    .checkSelfPermission(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);

    if(permissionCheck == PackageManager.PERMISSION_DENIED){

        //Should we show an explanation
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)){
            //Show an explanation
            final String message = "";
             Snackbar.make(coordinatorLayoutView,message,Snackbar.LENGTH_LONG)
                     .setAction("GRANT", new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
                         }
                     })
                     .show();

                }else{
                    //No explanation need,we can request the permission
                    ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
                }
            }
        }
sorak
  • 2,607
  • 2
  • 16
  • 24
Delphian
  • 1,650
  • 3
  • 15
  • 31

2 Answers2

56

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is not a dangerous permission. You do not need, or want, any of that code. Quoting the documentation for REQUEST_IGNORE_BATTERY_OPTIMIZATIONS:

Permission an application must hold in order to use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS. This is a normal permission: an app requesting it will always be granted the permission, without the user needing to approve or see it.

So, delete all that code.

I don't see my app in whitelist.

That is because the user did not add you to the whitelist, apparently.

Requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS grants you the authority, from a security standpoint, to start an activity with an ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent. Be sure to include your app's package as the Uri:

startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:"+getPackageName())));

The user will be taken to a screen where they can indicate that they are willing to suspend portions of Doze mode effects on your app.

How can I add the app in whitelist without banned?

If you do not want to be banned, do not do any of this. Have something in your app that starts an activity with an ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent. This leads the user to the overall list of apps, where the user can toggle which ones are and are not on the whitelist. This does not require any permission.

The act of requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in the manifest is what may get you banned.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • CommonsWare, thank you for detailed answer. It means I don't need the permission, only create intent? Am i right? – Delphian Jul 01 '17 at 15:39
  • 7
    @Delphian: If you want to use `ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`, to lead the user straight to a page specific for your app and the whitelist, you need to request `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` in the manifest, but you do not need to use runtime permissions code for it. If you want to avoid the permission, use `ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS` instead. – CommonsWare Jul 01 '17 at 15:42
  • 4
    this do not work in some vivo,oppo devices. – karan Dec 04 '21 at 09:48
8

Be aware of using Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent for the activity, this does not work on all phones and you will get a android.content.ActivityNotFoundException. In particular it does not work on Samsung phones running Android 6. The only combination that I have found works on these phones is to declare the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in the manifest, then launch an activity with intent Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS. I.e. the combination that is not liked by Google.

John Bunyan
  • 91
  • 1
  • 2
  • You've mentioned like adding `Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` to intent. is it like adding to `intent.setData` or some thing else? – Arun Jul 17 '18 at 12:32
  • @Arun - it's used in the setAction like so.... intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); – John Bunyan Jul 17 '18 at 14:44
  • So what is final solution? I am using oppo F1 (android 6) but even on adding permission it crashes – Kiran Bablani Jan 22 '20 at 07:46
  • 1
    Use of `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` violates the Play Store Content Policy regarding acceptable use cases, as described in [http://developer.android.com/training/monitoring-device-state/doze-standby.html. ] – Iman Marashi Apr 29 '20 at 20:04
  • > Note: Google Play policies prohibit apps from requesting direct exemption from Power Management features in Android 6.0+ (Doze and App Standby) unless the core function of the app is adversely affected. So I'm not sure it violates for apps that being affected by it... – Rock_Artist Aug 23 '20 at 05:45