REQUEST_IGNORE_BATTERY_OPTIMIZATIONS how to do it right

I have an IntentService task in the foreground, but in Android M + the task stops in Dosage mode. I read that Google is prohibited if the application uses the intention to set itself in the white list. But if I use permission and check GRANT or DENIED, I get the provided result, but nothing happens. I do not see my application in the white list. How to add an application to the white list without a ban? (I added permission to 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); } } } 
+5
source share
1 answer

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS not a dangerous permission. You do not need or do not need any of this code. Quoting documentation for REQUEST_IGNORE_BATTERY_OPTIMIZATIONS :

Application permission must be completed to use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS. This is a normal permission: the application requesting it will always receive permission, without the need for approval or viewing.

So delete all this code.

I do not see my application in the white list.

This is because the user has not added you to the white list, apparently.

The REQUEST_IGNORE_BATTERY_OPTIMIZATIONS query gives you security authority to start with a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent . Be sure to include the application package as Uri :

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

The user will be displayed on a screen where they can indicate that they are ready to pause parts of the effects of Doze mode in your application.

How to add an application to the white list without a ban?

If you do not want to be banned, do not do this. Do something in your application that starts the action with a ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent . This leads the user to a general list of applications where the user can switch which of them are not included in the white list. This does not require permission.

The act of requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in the manifest is something that might ban you .

+8
source

Source: https://habr.com/ru/post/1269397/


All Articles