Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SetAction in Xamarin

I'm trying to whitelist my app on Android 6.0 or greater. I have seen Android code to do this, but it doesn't translate in Xamarin and Xamarin documentation only tells you that SetAction takes a string as an argument, and then a link to Android documentation which doesn't end up being the same.

Here's the Android code that Xamarin will not accept

intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);

It doesn't like Settings. I've read the Xamarin documentation which says that SetAction() takes a string arg, and that's all they say and point you to Android documentation.

Note, I am calling this in a javascript interface class and I tried this but it doesn't work

class MyJSInterface : Java.Lang.Object
    {
    Context context;
    public MyJSInterface(Context context)
        {
            this.context = context;
        }
    [Export]
    [JavascriptInterface]
    public void SetDozeOptimization()
        {
            Toast.MakeText(context, "launch optimization", ToastLength.Short).Show();

            setDozeComplete = false;
            Intent intent = new Intent();
            String packageName = context.PackageName;
            PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
            if (pm.IsIgnoringBatteryOptimizations(packageName))
                intent.SetAction("ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS");
            else
            {
                intent.SetAction("ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS");
                intent.SetData(Android.Net.Uri.Parse("package:" + packageName));
            }
            context.StartActivity(intent);
        }

}

So what is the correct syntax to accomplish this?

like image 469
Bobh Avatar asked Oct 30 '25 21:10

Bobh


1 Answers

Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations:

  • android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings:

  • android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS

Example:

intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations);
intent.SetAction(Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings);
like image 167
SushiHangover Avatar answered Nov 02 '25 12:11

SushiHangover