打开位置设置页面或提示用户启用位置 [英] Opening Location Settings Page or Prompting user to Enable Location

查看:33
本文介绍了打开位置设置页面或提示用户启用位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在破解我的大脑并徒劳地使用谷歌搜索.我试图找到一种方法来提示用户通过直接转到设置页面或在屏幕上点击是来切换位置.我见过的所有代码似乎都不起作用.有没有人有什么工作.一个详细的例子将不胜感激.谢谢你.我真的是 Xamarin 开发的新手

i've been hacking my brain and googling away in vain. i am trying to find a way to prompt the user to switch location on either by going directly to the settings page or just tapping yes on the screen. all code i've seen doesn't seem to work. does any one have something that works. a detailed example will be much appreciated. thank you. I'm really new to Xamarin development

我更喜欢在 xamarin 表单上执行此操作的方法,但从提示 android 用户的内容开始,因为对于 iOS,我没有模拟器

i would prefer a way to do it on xamarin forms, but starting with something that will prompt android user, because for iOS i have no simulator

.

推荐答案

所以在浏览了互联网上的几个教程和答案后,我终于找到了我想要实现的目标,

so after going through several tutorials and answers all over the internet i finally was able to find to accomplish what i wanted to achieve,

我使用依赖服务作为此处指出的答案之一如何从我们的 xamarin 应用程序中打开设置

i used a dependency service as one of the answers indicated here How to open setting from our application in xamarin

有些事情没有被提及,例如注册接口以便在特定于平台的项目中使用它.

there are few things that were not mentioned like registering the interface in order to use it in platform specific projects.

这是任何需要它的人的代码

here is the code for anyone who needs it

界面:我调用了我的接口 ILocSettings.cs

the Interface : I called my Interface ILocSettings.cs

using System;
using System.Collections.Generic;
using System.Text;


[assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]
namespace DE2
{
    public interface ILocSettings
    {
        void OpenSettings();

    }

有一个按钮的表单我称之为 DataEntryForm

the form that has a button I called it DataEntryForm

DataEntryForm.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DE2.DataEntryForm"
             >
    <ContentPage.Content>
        <StackLayout>

            <Button x:Name="TurnLocationOn"
                    Text="Turn On Location"
                    Clicked="TurnLocationOn_OnClicked"/>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

然后是 DataEntryForm.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Plugin.pbXSettings;
using Plugin.Geolocator;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Plugin.DeviceInfo;
using Plugin.DeviceInfo.Abstractions;

[assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]

namespace DE2
{

    using Xamarin.Forms.PlatformConfiguration;

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DataEntryForm : ContentPage
    {
        public DataEntryForm ()
        {
            InitializeComponent ();

        }


        private async void  TurnLocationOn_OnClicked(object sender, global::System.EventArgs e)
        {

            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<global::DE2.ILocSettings>().OpenSettings();



                }
                else
                {
                    DisplayAlert("Device", "You are using some other shit", "YEP");
                }
            }
            else
            {
                DisplayAlert("Alert","User Denied Permission","OK");
            }


            // 
        }
    }
}

然后我将这个类放置在 Android 特定平台上 LocationZ.cs

Then I have this Class Placed on the Android Specific Platform LocationZ.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Locations;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Android;
using Xamarin.Forms;
using DE2;
using DE2.Droid;


//[assembly: Xamarin.Forms.Dependency(typeof(ILocSettings))]

//Notice the use of LocationZ in registering below instead of ILocSettings

[assembly: Xamarin.Forms.Dependency(typeof(LocationZ))]

namespace DE2.Droid
{
    using System.Runtime.Remoting.Messaging;

    using Android.Support.V4.View;
    using Android.Support.V7.App;

    using Xamarin.Forms;
    using DE2;


    public class LocationZ : ILocSettings
    {
        public void OpenSettings()
        {
            LocationManager LM = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);


            if (LM.IsProviderEnabled(LocationManager.GpsProvider)==false)
            {


                            Context ctx = Forms.Context;
                            ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));


            }
            else
            {
                //this is handled in the PCL
            }
        }


    }
}

`

这篇关于打开位置设置页面或提示用户启用位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆