简体   繁体   English

我的应用由于Android上的权限问题而终止

[英]My app is terminating due to permission issue on Android

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: 我正在编写此代码以获取gps位置,已在Android属性上标记了ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION,并且还确认它是否在AndroidManifest.xml中,但该应用程序正在终止:

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.048 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. 10-08 19:52:50.103 I / mono-stdout(2905):位置权限被拒绝,无法获取位置异步。

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. 如果我转到“配置”>“应用程序”>“权限”并提供位置权限,则它可以正常工作,但在安装时并没有要求权限。

The code: 编码:

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? 谁能看到我在做什么错?

Starting with Android Marshmallow, you need to request permissions from the user as-well-as marking it in the manifest. 从Android Marshmallow开始,您需要在清单中标记用户以及向用户请求权限。 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 provides some documentation on how to do this here . Xamarin提供了有关如何做到这一点的一些文件在这里

EDIT: Newer documentation is also available from Microsoft 编辑:也可以从Microsoft获得更新的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序因热重启的“看门狗”错误而终止 - App terminating due to 'Watchdog' error with Hot Restart 为什么我的 android xamarin 应用在权限被拒绝时自动关闭? - Why my android xamarin app close automatically when permission is denied? 由于 StackOverflowException 导致递归因子终止 - Recursion Factorial terminating due to StackOverflowException .NETCore应用程序因StackOverflowException而终止 - .NETCore Application terminating due to StackOverflowException 我是否必须统一请求我的 Android 应用程序的文件操作权限? - Do I have to ask permission for file manipulation for my Android app in unity? 由于调用GetType()的StackOverflowException,进程正在终止 - Process is terminating due to StackOverflowException on calling GetType() ASP.NET 由于超时而终止 - ASP.NET Terminating due to Timeout “进程由于 StackOverflowException 而终止”,其原因在以下代码中 - "Process is terminating due to StackOverflowException", reason for it in the following code 我的应用程序的任务栏问题 - Taskbar Issue for My App 如何禁用挂起|| 结束|| Windows 8 Pro上Metro Metro App的终止事件 - How to disable Suspending || Closing || Terminating event for my Metro Style App on Windows 8 Pro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM