-1

When I run my application on Android 7. It returns this error.

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)

But on Android 6, everything works well.

I tried to do it as indicated here: Android “gps requires ACCESS_FINE_LOCATION” error

But, my Android Studio gives out such parameters in the Manifest.

In the Manifesto, rights are defined as follows:

<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>
Anton Platonov
  • 379
  • 4
  • 15

3 Answers3

0

Since Android 6 you need to ask permissions for location at runtime. More can be found here Request runtime permissions

p.mathew13
  • 920
  • 8
  • 16
0

All dangerous permission should be requested at the runtime apart from mentioning them in the manifest.

In the code, where every you are trying to access Location, you need to first check if your app has been granted access to Location as below:

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

If you happen to ask user to grant location permission, you will receive the callback here if users grants/not-grant the 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.
    }
}

For more information, please refer here.

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
0

That's how I finally did it:

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)
    }
Anton Platonov
  • 379
  • 4
  • 15