Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACCESS_FINE_LOCATION being granted in manifest, yet always getting permission denied?

Tags:

android

I'm currently trying to get the latitude and longitude of the user's current location. I have enabled the following permissions in the manifest. TargetSDK and min sdk are 23

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.name.app">


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false" />


    <activity
        android:name=".SplashScreen"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainMenu" />
    <activity
        android:name=".NeedToKnow" />
    <activity
        android:name=".SelectTopic" />
    <activity
        android:name=".DisplayTopicInformation"/>
    <activity
        android:name=".SelectCheckList" />
    <activity
        android:name=".ViewCheckList"/>
    <activity
        android:name=".FindLocation"></activity>
</application>

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


</manifest>

In order to do so I understand that I need to perform a runtime check for the permissions. However when I perform the following I always get -1 in return. (PERMISSION_DENIED)

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

Is there a step I am missing when trying to access current locations?

Full code is here:

private String getZipcodeFromCurrentLocation() {

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

    String zip = null;
    Location location = null;
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();

            Log.i("updated lat", String.valueOf(latitude));
            Log.i("updated lng", String.valueOf(longitude));
        }


    };

    try {
        if (PackageManager.PERMISSION_GRANTED == permissionCheck) {
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }

    } catch (SecurityException e){
        e.printStackTrace();
    }
    return zip;

}
like image 903
Airagale Avatar asked Feb 03 '16 21:02

Airagale


People also ask

What is the purpose of setting the user permission for Access_fine_location?

If a feature in your app absolutely requires access to precise location using the ACCESS_FINE_LOCATION permission, you can ask the user to allow your app to access precise location.


1 Answers

If user has disabled location permission for your app in the phone's settings, then you have to request that permission. Below you'll find a sample piece of code on how to do it.

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

public class MyActivity extends Activity implements  ActivityCompat.OnRequestPermissionsResultCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        if(permissionGranted) {
            // {Some Code}
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 200: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // {Some Code}
                }
            }
        }
    }
}

Also I've build a class that handles all of the above and also returns user's location. Here's the link: https://www.dropbox.com/s/2mkyjok6mpna2yw/GPS.java?dl=0.

like image 62
Arnold Plakolli Avatar answered Oct 22 '22 13:10

Arnold Plakolli