question

6666666 avatar image
0 Votes"
6666666 asked LeonLu-MSFT answered

How to request `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission in xamarin.essentials.permission?

how to request this permission? in android?

I want to connect to the internet while is in background running. but it failed.

after search I know I have to request this permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS how to request it?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is not a dangerous permission. You do not need, or want, any of that code(do not need request runtime permission). Here is Google's document.

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.

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, Here is code

private void Button1_Click(object sender, System.EventArgs e)
        {
          
          
            Intent intent = new Intent();
            string packageName = this.PackageName;
            PowerManager pm = (PowerManager)this.GetSystemService(Context.PowerService);
            if (pm.IsIgnoringBatteryOptimizations(packageName))
                intent.SetAction(Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings);
            else
            {
                intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations);
                intent.SetData(Uri.Parse("package:" + packageName));
            }
            StartActivity(intent);
           
        }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.