缺少LocationManager所需的权限

时间:2019-07-06 11:35:30

标签: android android-permissions

Android Studio 3.4。

Android 6.0上的设备

在manifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.myproject">


    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_GPS"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="md.qsystems.android.tango.permission.MAPS_RECEIVE"/>
    <!-- MAP -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

在我的简单课堂(无活动/片段)中,我有:

public class LocationHelper {

if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
            }

我有编译错误:

Missing permissions required by LocationManager.requestLocationUpdates: android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION 

}

3 个答案:

答案 0 :(得分:1)

您需要对清单文件拥有这两个权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

因此,请检查清单文件并确保它们都包含。

答案 1 :(得分:1)

在需要权限时调用此checkAndRequestPermissions()方法

public boolean checkAndRequestPermissions() {
    int internet = ContextCompat.checkSelfPermission(mContext, 
    Manifest.permission.INTERNET);
    int loc = ContextCompat.checkSelfPermission(mContext, 
    Manifest.permission.ACCESS_COARSE_LOCATION);
    int loc2 = ContextCompat.checkSelfPermission(mContext, 
    Manifest.permission.ACCESS_FINE_LOCATION);
    List<String> listPermissionsNeeded = new ArrayList<>();

    if (internet != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.INTERNET);
    }
    if (loc != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    }
    if (loc2 != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions((Activity) mContext, listPermissionsNeeded.toArray
                (new String[listPermissionsNeeded.size()]), 1);
        return false;
    }
    return true;
}

答案 2 :(得分:0)

在AndroidManifest文件中添加ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

在运行时请求使用设备当前位置的权限,如下所示:

if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
      return;
}else{
  // Write you code here if permission already given.
  // Call LocationHelper 
} 

用户是否允许使用设备当前位置时的句柄:

   @Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
           if (grantResults.length > 0  && grantResults[0] == 
       PackageManager.PERMISSION_GRANTED) {
       // Write you code here if permission already given.
       // Call LocationHelper 
}                           }else{
                        // you must ask location permission again
                        }
}