Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACCESS_FINE_LOCATION permission error emulator only

I'm having a weird error while using the emulator ONLY. I found one question that has the same issue 9 months ago with no answers at all...

I'm using google play services locations to get locations in my app and I'm sure of my manifest permissions and everything works perfectly on my phone, the problem occurs when using an emulator only, and I tried different emulators on both my iMac and Windows machines.

I doubt that emulators don't support this basic function!

here is my manifest code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.orderme.ordermeandroid" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and here is where the exception is firing:

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,mLocationRequest,this);

stack trace:

08-26 14:01:19.699  10157-10157/com.orderme.ordermeandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.orderme.ordermeandroid, PID: 10157
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
        at android.os.Parcel.readException(Parcel.java:1599)
        at android.os.Parcel.readException(Parcel.java:1552)
        at com.google.android.gms.location.internal.zzg$zza$zza.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzi.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzj.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
        at com.google.android.gms.common.api.zzc$zza.zzb(Unknown Source)
        at com.google.android.gms.common.api.zzf.zza(Unknown Source)
        at com.google.android.gms.common.api.zzf.zzb(Unknown Source)
        at com.google.android.gms.common.api.zzi.zzb(Unknown Source)
        at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
        at com.orderme.ordermeandroid.Main.MainActivity.onConnected(MainActivity.java:184)
        at com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
        at com.google.android.gms.common.api.zzg.zznt(Unknown Source)
        at com.google.android.gms.common.api.zzg.onConnected(Unknown Source)
        at com.google.android.gms.common.api.zzi$2.onConnected(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzg.zzoD(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zza.zzs(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzc.zzoF(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
like image 804
Hassan Khallouf Avatar asked Aug 26 '15 10:08

Hassan Khallouf


People also ask

How do I allow location permissions?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do I grant background location permissions in Android?

Permission dialog changes In order to enable background location access, users must set the Allow all the time option for your app's location permission on a settings page, as described in the guide on how to Request background location.

What are the required permissions for Android location Manager?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API. android.


4 Answers

My guess is that you are running this on an Android 6.0+ emulator and you have a targetSdkVersion of 23 or higher.

In that case, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION are part of the Android 6.0 runtime permission system. Either revise your app to participate in this system, or drop your targetSdkVersion below 23.

like image 91
CommonsWare Avatar answered Oct 29 '22 18:10

CommonsWare


in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app

How to check if you have the permission:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        android.Manifest.permission.ACCESS_FINE_LOCATION);

How to request the permissions you need :

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

    // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}

How to access the result of the request :

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the task you need to do.

        } else {

            // permission denied, boo! Disable the functionality that depends on this permission.
        }
        return;
    }
}
}

Source : http://developer.android.com/training/permissions/requesting.html

Learn more about permissions : https://guides.codepath.com/android/Understanding-App-Permissions

like image 27
Fabich Avatar answered Oct 29 '22 16:10

Fabich


You can not get the current location from emulator. But you can emulate it to test your code. Here you can find how to emulate.

EDIT1: There is a note here

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)

You should use only ACCESS_FINE_LOCATION permission. Maybe this helps.

like image 24
kgnkbyl Avatar answered Oct 29 '22 16:10

kgnkbyl


The issue was caused by a new feature for Android 6.0. hope its work...

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

http://developer.android.com/training/permissions/requesting.html

To get around the issue when the permission is not explicitly granted, I had put in a check to wrap location service calls (as per the Google Documentation, slightly modified).

int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);

if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
    //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
}

The Google Documentation also steps through how to request for the permissions that are needed.

like image 24
Mahendran Candy Avatar answered Oct 29 '22 17:10

Mahendran Candy