簡體   English   中英

如何使用xamarin打開android中的位置設置

[英]How to open location settings in android using xamarin

我嘗試使用教程在 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 位置設置..

在 Android 上打開其他活動是通過 Intents 完成的。 要打開位置設置,您可以使用 ACTION_LOCATION_SOURCE_SETTINGS 意圖。

像這樣的東西可能會起作用:

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);

有一個 package 可以更輕松地打開應用程序設置。

寫吧:

CrossPermissions.Current.OpenAppSettings();

插件名稱 Plugin.Permissions:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM