我正在尝试获取用户在地图上的位置,但这使我处于“中断模式”,而且我不知道问题是否出在“ OnRequestPermissionsResult”函数中。
这是我的MainActivity.cs
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Environment = System.Environment;
using System.IO;
namespace TravelRecordApp.Droid
{
[Activity(Label = "TravelRecordApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
string dbName = "travel_db.sqlite";
string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string fullPath = Path.Combine(folderPath, dbName);
LoadApplication(new App(fullPath));
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
还有我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.travelrecordapp" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="TravelRecordApp.Android">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyDNzCR_BT0ocNpQt_FYaDc7t_SOSFuanrM" />
</application>
</manifest>
答案 0 :(得分:0)
您是否请求运行时权限,在Android6.0之后,您应该请求runtime permissions和official doucument
您可以使用nugetpackage Plugin.Permissions 来请求运行时权限, 请参阅Plugin.Permissions
当您需要请求位置许可时,您可以这样做:
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
}
if (status == PermissionStatus.Granted)
{
// do something you want
}
else if (status != PermissionStatus.Unknown)
{
//location denied
}
}
catch (Exception ex)
{
//Something went wrong
}
在您的MainActivity中:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
答案 1 :(得分:0)
我建议您使用本机代码而不是跨平台的解决方案来检查运行时权限,对于相同的问题,我有一个可行的解决方案,如下所示:
在您的OnCreate方法中检查现有权限:
if (!(CheckPermissionGranted(Manifest.Permission.AccessCoarseLocation) &&
CheckPermissionGranted(Manifest.Permission.AccessFineLocation)))
{
RequestLocationPermission();
}
else
{
InitializeLocationManager();
}
InitPageWidgets();
授予“检查权限”的方法如下:
[Export]
public bool CheckPermissionGranted(string Permissions)
{
// Check if the permission is already available.
if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
{
return false;
}
else
{
return true;
}
}
“请求”许可权代码如下所示:
private void RequestLocationPermission()
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation))
{
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
ActivityCompat.RequestPermissions(this, PermissionsLocation, REQUEST_LOCATION);
}
else
{
// Camera permission has not been granted yet. Request it directly.
ActivityCompat.RequestPermissions(this, PermissionsLocation, REQUEST_LOCATION);
}
}
一旦接受或拒绝此权限,该方法就会被调用:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions,
Android.Content.PM.Permission[] grantResults)
{
Log.Info(Tag, "onRequestPermissionResult");
if (requestCode == RequestPermissionsRequestCode)
{
if (grantResults.Length <= 0)
{
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.Info(Tag, "User interaction was cancelled.");
}
else if (grantResults[0] == PermissionChecker.PermissionGranted)
{
// Permission was granted.
Service.RequestLocationUpdates();
}
else
{
// Permission denied.
SetButtonsState(false);
Toast.MakeText(this, "Permission Denied", ToastLength.Long).Show();
}
}
}