从AlertBox实际启用GPS

时间:2017-05-02 21:45:22

标签: c# android xamarin gps xamarin.android

我在Xamarin Platform Visual Studio上使用C#构建了一个Android应用程序。 我正在尝试构建一个AlertDialog,允许用户直接从应用程序启用GPS。甚至将他送到位置设置。到目前为止,我有这个代码,检查GPS是否启用。我需要的是如何在AlertDialog上制作OnClick按钮,甚至可以从应用程序启用GPS或打开设置。

 LocationManager lm = (LocationManager)GetSystemService(Context.LocationService);
        bool gps_enabled = false;
        bool network_enabled = false;
        gps_enabled = lm.IsProviderEnabled(LocationManager.GpsProvider);
        network_enabled = lm.IsProviderEnabled(LocationManager.NetworkProvider);
if (!gps_enabled && !network_enabled)
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.SetTitle("Warning");
            dialog.SetMessage("Your phone location is not enabled" + "\n" + "Please enable your location and run SpeedReport again");
            dialog.SetPositiveButton("Enable", (object sender1, DialogClickEventArgs e1) => {/*i guess i need to enter some code here*/});

1 个答案:

答案 0 :(得分:1)

看一下这个: https://developer.xamarin.com/api/field/Android.Provider.Settings.ActionLocationSourceSettings/

你必须写一些类似的东西:

var locationIntent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings);
locationIntent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(locationIntent);