我的应用由于Android上的权限问题而终止 [英] My app is terminating due to permission issue on Android

查看:122
本文介绍了我的应用由于Android上的权限问题而终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写此代码以获取gps位置, 在Android属性上标记为ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION,我还确认它是否位于AndroidManifest.xml中,但应用程序正在终止:

I'm writing this code for getting gps location, I've marked ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION on Android properties and I also confirmed if it was in AndroidManifest.xml but the application is terminating:

10-08 19:52:50.048 I/mono-stdout(2905):当前没有位置权限,正在请求权限当前没有位置权限,正在请求权限

10-08 19:52:50.048 I/mono-stdout( 2905): Currently does not have Location permissions, requesting permissionsCurrently does not have Location permissions, requesting permissions

拒绝位置权限,无法获取位置异步.10-08 19:52:50.103 I/mono-stdout(2905):位置权限被拒绝,无法获取位置异步.

Location permission denied, can not get positions async.10-08 19:52:50.103 I/mono-stdout( 2905): Location permission denied, can not get positions async.

如果我转到配置">应用程序">权限"并授予位置权限,则它可以正常运行,但在安装时并没有要求权限.

If I go to Configuration>Applications>Permissions and give location permission it works properly but it isn't asking for permissions at the installation time.

代码:

using Plugin.Geolocator;
using Plugin.Geolocator.Abstractions;
using System;
using Xamarin.Forms;

namespace AppName
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        Position position;
        private async void GetPosition(object sender, EventArgs e)
        {
            try
            {
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 50;
                position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
            }
            catch(Exception)
            {
                throw;
            }
            finally { 
                LongitudeLabel.Text = string.Format("{0:0.0000000}", position.Longitude);
                LatitudeLabel.Text = string.Format("{0:0.0000000}", position.Latitude);
            }
        }
    }
}

任何人都可以看到我在做什么吗?

Can anyone see what I'm doing wrong?

推荐答案

从Android Marshmallow开始,您需要向用户请求权限,以及在清单中对其进行标记.这样做将向用户显示提示,要求您访问他们可以允许或拒绝的资源(在这种情况下为位置).您应该有适当的代码来处理这两种情况.

Starting with Android Marshmallow, you need to request permissions from the user as-well-as marking it in the manifest. Doing this will show the user a prompt asking to access the resource (location in this case) that they can either allow or deny. You should have code in place handling both situations.

Xamarin提供了有关如何执行此操作的一些文档,此处.

Xamarin provides some documentation on how to do this here.

也可以从微软

这篇关于我的应用由于Android上的权限问题而终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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