ACCESS_FINE_LOCATION permission error emulator only

时光怂恿深爱的人放手 提交于 2019-12-17 10:40:52

问题


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)

回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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

some times manifest have a missing line granting the permission of ACCESS_FINE_LOCATION. so you need to mention it in the manifest.

I did this and my problem was solved.

This generally happens because we have given the permission in the manifest so the app would create a problem as it would not be allowed to access the permissions and the file would crash.

<?xml version="1.0" encoding="utf-8"?>


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_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>



来源:https://stackoverflow.com/questions/32224534/access-fine-location-permission-error-emulator-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!