位置权限问题适用于Android 6和7/8

时间:2018-12-03 14:16:01

标签: android google-maps kotlin

当我在Android 7上运行应用程序时,它将返回此错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ruslan.testmapagain, PID: 20482
    java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
        at com.google.maps.api.android.lib6.impl.bf.c(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):566)
        at com.google.android.gms.maps.internal.l.a(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):361)
        at fu.onTransact(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):4)
        at android.os.Binder.transact(Binder.java:499)
        at com.google.android.gms.internal.maps.zza.transactAndReadExceptionReturnVoid(Unknown Source)
        at com.google.android.gms.maps.internal.zzg.setMyLocationEnabled(Unknown Source)
        at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source)
        at com.example.ruslan.testmapagain.MapsActivity.onMapReady(MapsActivity.kt:151)
        at com.google.android.gms.maps.zzak.zza(Unknown Source)
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:499)
        at ft.b(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):15)
        at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):4)
        at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14574051@14.5.74 (040408-219897028):4)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)

但是在Android 6上,一切正常。

我尝试按照此处指示进行操作:Android “gps requires ACCESS_FINE_LOCATION” error

但是,我的Android Studio在清单中给出了此类参数。

在宣言中,权利的定义如下:

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

3 个答案:

答案 0 :(得分:0)

从Android 6开始,您需要在运行时询问权限。在这里Request runtime permissions

中可以找到更多内容

答案 1 :(得分:0)

除了在清单中提及所有dangerous permission之外,还应该在运行时请求所有here

在代码中,每个您尝试访问位置的位置,都需要首先检查您的应用是否已被授予访问位置的权限,如下所示:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
    mMap.setMyLocationEnabled(true);
} else {
    // Show rationale and request permission.
}

如果您碰巧要求用户授予位置权限,那么如果用户授予/不授予该权限,您将在此处收到回调:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_LOCATION_REQUEST_CODE) {
      if (permissions.length == 1 &&
          permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
          grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
      // Permission was denied. Display an error message.
    }
}

有关更多信息,请参阅{{3}}。

答案 2 :(得分:0)

这就是我最终做到的方式:

private fun setupPermissions() {

        val permission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)

        if (permission != PackageManager.PERMISSION_GRANTED) {
            makeRequest()
        }
    }

    private fun makeRequest() {
        ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                RECORD_REQUEST_CODE)
    }