【问题标题】:How to open location settings in android using xamarin如何使用 xamarin 在 android 中打开位置设置
【发布时间】:2021-05-28 08:19:04
【问题描述】:

我尝试使用this教程在android中打开位置设置,但是在这个方法中:

public void OpenSettings()
    {
        
    }

我不知道在打开的位置设置中写什么。

我需要额外的库吗?

我写了这段代码:

public interface ILocSettings
    {
        void OpenSettings();

    }

    public void OpenSettings()
    {
        
    }

在GPS按钮方法中我写了这段代码:

 var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK", "CANCEL");
                if (myAction)
                {
                    if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
                    {

                        //DependencyService.Get<ISettingsService>().OpenSettings();
                        global::Xamarin.Forms.DependencyService.Get<ILocSettings>().OpenSettings();
                    }
                    else
                    {
                        _ = DisplayAlert("Device", "You are using some other shit", "YEP");
                    }
                }
                else
                {
                    _ = DisplayAlert("Alert", "User Denied Permission", "OK");
                }

所以消息出现了,但我不知道在 OpenSettings() 中写什么来打开手机上的 GPS 位置设置..

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:

    有一个软件包可以更轻松地打开应用设置。

    只写:

    CrossPermissions.Current.OpenAppSettings();
    

    插件名称 Plugin.Permissions :)

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    在 Android 上打开其他 Activity 是通过 Intents 完成的。要打开位置设置,您可以使用 ACTION_LOCATION_SOURCE_SETTINGS Intent。

    这样的事情可能会起作用:

    var intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
    Android.App.Application.Context.StartActivity(intent);
    

    如果 Android 不喜欢 Application Context,您可能需要解析顶部 Activity 并将其用作您的 Context。

    如果您使用的是 Xamarin.Essentials,您可以通过以下方式获得顶级活动:Platform.CurrentActivity:

    var intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
    Platform.CurrentActivity.StartActivity(intent);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-07
      • 2021-04-15
      • 1970-01-01
      • 2018-10-14
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多