Forum Xamarin.Android

The Battery optimizations not set in the lasted version OS 10 / 11

I fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent and set Allow, but when i go to setting the app not change from mode battery save.
This is a problem because i have a foreground service for get locations, and the request location not return after 2 min background execution.
I used the api xamarin essentials and native android get locations for test, in boths i have the same behavior.
For get location:

 var ts = new TimeSpan();
                    ts = TimeSpan.FromSeconds(15);
                    var request = new GeolocationRequest(GeolocationAccuracy.Medium);
                    var location = await Geolocation.GetLocationAsync(request);

For set ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS :

if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                var packageName = ApplicationContext.PackageName;
                var pm = (PowerManager)this.GetSystemService(Context.PowerService);
                if (!pm.IsIgnoringBatteryOptimizations(packageName))
                {
                    BatteryOptimizationsIntent = new Intent();
                    BatteryOptimizationsIntent.AddFlags(ActivityFlags.NewTask);

                    BatteryOptimizationsIntent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations);
                    BatteryOptimizationsIntent.SetData(Android.Net.Uri.Parse("package:" + packageName));
                    StartActivity(BatteryOptimizationsIntent);
                }
            }

I gracefull for any comment or help****

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 15

    I created a basic demo to test the code on Android 10, it works fine. Did you open the Setting page before calling the method? There may be a delay in updating the settings page.

    After calling the function code, try to reopen the setting page to check the status of battery optimization. You'll find the app is 'Not optimized'.

  • ciullosanchezciullosanchez Member ✭✭

    Hello Jarvan! thanks for your reply.

    For answer your question, i don't see the change even with reopen the settings. In cellphone redmi 9 OS 10. And the problem is because before 2 min my app is deactivate for the battery optimization and not longer get locations.

    Your demo is in xamarin android, rigth?

    I test in emulator and its works. You have any idea why ndo't work in the my specific device?

    I gratefull any comment!

  • JarvanJarvan Member, Xamarin Team Xamurai

    Your demo is in xamarin android, rigth?

    Yes, I test the code in a Xamarin.Android project.

    I test in emulator and its works. You have any idea why ndo't work in the my specific device?

    This may be caused by the special setting of the XiaoMi phone, try testing the sample on another brand device to vertify that.

  • ciullosanchezciullosanchez Member ✭✭

    @Jarvan said:

    Your demo is in xamarin android, rigth?

    Yes, I test the code in a Xamarin.Android project.

    I test in emulator and its works. You have any idea why ndo't work in the my specific device?

    This may be caused by the special setting of the XiaoMi phone, try testing the sample on another brand device to vertify that.

    Hello, yes! its work in samsung.

    My question is if what it a posibly solution? or definly in some brands my app not working... I want my app works independently of that.

    Someone have the same problem?

  • ciullosanchezciullosanchez Member ✭✭

    Thank u Javar for you answer and you time.

Sign In or Register to comment.