Issue
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?
Solution
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);
Answered By - SushiHangover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.