我试图在运行时获取ACCESS_FINE_LOCATION的权限(来自运行Android版本高于6.0的用户)。这在我的主应用程序中是不成功的,所以我尝试制作一个测试应用程序,其唯一目的是请求用户的位置权限。
我遇到的问题是没有向用户显示任何权限对话框 - 从logcat我可以看到'onRequestPermissionsResult'方法正在立即运行(不要求用户接受权限),并且它显示了权限未被授予。
在查看了有关此主题的其他问题之后,我仔细检查了“使用权限”行是否在我的Android Manifest XML文件中,但找不到任何其他解决方案。
这是我的代码:
package com.user.testapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION_ID = 1;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkLocationPermissions();
}
private void checkLocationPermissions(){
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.i(TAG, "Device version above 6.0 - Requesting location permissions.");
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_ID);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_LOCATION_ID: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permissions granted successfully!");
} else {
Log.i(TAG, "Permissions were not granted.");
}
return;
}
}//end switch
}
}//end class
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.user.testapplication">
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
和logcat的相关行:
11-27 13:27:47.752 31839-31839/com.user.testapplication I/MainActivity: Device version above 6.0 - Requesting location permissions.
11-27 13:27:47.881 31839-31839/com.user.testapplication I/MainActivity: Permissions were not granted.
非常感谢任何帮助 - 我想我可能只是缺少一些东西。
在onStart()方法中使用它,它将解决您的问题。
//This checks for the permission
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)){
// You can show your dialog message here but instead I am
// showing the grant permission dialog box
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
10);
}
else{
//Requesting permission
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
10);
}
}
没有显示Dialog
框的原因是AndroidManifest
中的拼写错误,你可以看到许可是拼写android-permisssion
而不是android-permission
。
我也遇到了同样的问题我的问题是我在清单中写了一行
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
代替
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />