Hello 德哥,
Welcome to our Microsoft Q&A platform!
Your achievement comes from a exploite a bug in the power manager widget, But this Bug is fixed, Please see the SilithCrowe's Comment.
https://stackoverflow.com/a/5305835
If you still want to translate it to C# code. Here is your needs.
class GPSService : IGPSService
{
public void turnGPSOff()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.PutExtra("enabled", true);
MainActivity.Instance.SendBroadcast(intent);
bool gps_enabled = false;
if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
{
// This is new method provided in API 28
LocationManager lm = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
try
{
gps_enabled = lm.IsProviderEnabled(LocationManager.GpsProvider);
if (!gps_enabled)
{
//if gps is disabled
Intent poke = new Intent();
poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.AddCategory(Intent.CategoryAlternative);
poke.SetData(Android.Net.Uri.Parse("3"));
MainActivity.Instance.SendBroadcast(poke);
}
}
catch (Exception ex) { }
}
else
{
// This is Deprecated in API 28
string provider = Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.LocationProvidersAllowed);
if (!provider.Contains("gps"))
{ //if gps is disabled
Intent poke = new Intent();
poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.AddCategory(Intent.CategoryAlternative);
poke.SetData(Android.Net.Uri.Parse("3"));
MainActivity.Instance.SendBroadcast(poke);
}
}
}
public void turnGPSOn()
{
string provider = Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.LocationProvidersAllowed);
if (!provider.Contains("gps"))
{ //if gps is disabled
Intent poke = new Intent();
poke.SetClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.AddCategory(Intent.CategoryAlternative);
poke.SetData(Android.Net.Uri.Parse("3"));
MainActivity.Instance.SendBroadcast(poke);
}
}
}
PS: MainActivity.Instance
is a static property set value in the OnCreate
method.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Instance = this;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
}
But I want to recommand you to use following ways to enable the GPS location. It will open the location settings in System settings.
private void ButtonEnableGPS_Click(object sender, System.EventArgs e)
{
//throw new System.NotImplementedException();
LocationManager lm = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
bool gps_enabled = false;
try
{
gps_enabled = lm.IsProviderEnabled(LocationManager.GpsProvider);
if (!gps_enabled)
{
//if gps is disabled
Intent intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings);
this.StartActivity(intent);
}
}
catch (Exception ex) { }
}
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.