Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This

Android gps requires ACCESS_FINE_LOCATION error, even though my manifest file contains this

ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, and WRITE_EXTERNAL_STORAGE are all part of the Android 6.0 runtime permission system. In addition to having them in the manifest as you do, you also have to request them from the user at runtime (using requestPermissions()) and see if you have them (using checkSelfPermission()).

One workaround in the short term is to drop your targetSdkVersion below 23.

But, eventually, you will want to update your app to use the runtime permission system.

For example, this activity works with five permissions. Four are runtime permissions, though it is presently only handling three (I wrote it before WRITE_EXTERNAL_STORAGE was added to the runtime permission roster).

/***
Copyright (c) 2015 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.permmonger;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final String[] INITIAL_PERMS={
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CONTACTS
};
private static final String[] CAMERA_PERMS={
Manifest.permission.CAMERA
};
private static final String[] CONTACTS_PERMS={
Manifest.permission.READ_CONTACTS
};
private static final String[] LOCATION_PERMS={
Manifest.permission.ACCESS_FINE_LOCATION
};
private static final int INITIAL_REQUEST=1337;
private static final int CAMERA_REQUEST=INITIAL_REQUEST+1;
private static final int CONTACTS_REQUEST=INITIAL_REQUEST+2;
private static final int LOCATION_REQUEST=INITIAL_REQUEST+3;
private TextView location;
private TextView camera;
private TextView internet;
private TextView contacts;
private TextView storage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

location=(TextView)findViewById(R.id.location_value);
camera=(TextView)findViewById(R.id.camera_value);
internet=(TextView)findViewById(R.id.internet_value);
contacts=(TextView)findViewById(R.id.contacts_value);
storage=(TextView)findViewById(R.id.storage_value);

if (!canAccessLocation() || !canAccessContacts()) {
requestPermissions(INITIAL_PERMS, INITIAL_REQUEST);
}
}

@Override
protected void onResume() {
super.onResume();

updateTable();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions, menu);

return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.camera:
if (canAccessCamera()) {
doCameraThing();
}
else {
requestPermissions(CAMERA_PERMS, CAMERA_REQUEST);
}
return(true);

case R.id.contacts:
if (canAccessContacts()) {
doContactsThing();
}
else {
requestPermissions(CONTACTS_PERMS, CONTACTS_REQUEST);
}
return(true);

case R.id.location:
if (canAccessLocation()) {
doLocationThing();
}
else {
requestPermissions(LOCATION_PERMS, LOCATION_REQUEST);
}
return(true);
}

return(super.onOptionsItemSelected(item));
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
updateTable();

switch(requestCode) {
case CAMERA_REQUEST:
if (canAccessCamera()) {
doCameraThing();
}
else {
bzzzt();
}
break;

case CONTACTS_REQUEST:
if (canAccessContacts()) {
doContactsThing();
}
else {
bzzzt();
}
break;

case LOCATION_REQUEST:
if (canAccessLocation()) {
doLocationThing();
}
else {
bzzzt();
}
break;
}
}

private void updateTable() {
location.setText(String.valueOf(canAccessLocation()));
camera.setText(String.valueOf(canAccessCamera()));
internet.setText(String.valueOf(hasPermission(Manifest.permission.INTERNET)));
contacts.setText(String.valueOf(canAccessContacts()));
storage.setText(String.valueOf(hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)));
}

private boolean canAccessLocation() {
return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
}

private boolean canAccessCamera() {
return(hasPermission(Manifest.permission.CAMERA));
}

private boolean canAccessContacts() {
return(hasPermission(Manifest.permission.READ_CONTACTS));
}

private boolean hasPermission(String perm) {
return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
}

private void bzzzt() {
Toast.makeText(this, R.string.toast_bzzzt, Toast.LENGTH_LONG).show();
}

private void doCameraThing() {
Toast.makeText(this, R.string.toast_camera, Toast.LENGTH_SHORT).show();
}

private void doContactsThing() {
Toast.makeText(this, R.string.toast_contacts, Toast.LENGTH_SHORT).show();
}

private void doLocationThing() {
Toast.makeText(this, R.string.toast_location, Toast.LENGTH_SHORT).show();
}
}

(from this sample project)

For the requestPermissions() function, should the parameters just be "ACCESS_COARSE_LOCATION"? Or should I include the full name "android.permission.ACCESS_COARSE_LOCATION"?

I would use the constants defined on Manifest.permission, as shown above.

Also, what is the request code?

That will be passed back to you as the first parameter to onRequestPermissionsResult(), so you can tell one requestPermissions() call from another.

gps location provider requires ACCESS_FINE_LOCATION permission for android 6.0

Since 6.0 some permissions are considered as "dangerous" (FINE_LOCATION is one of them).

To protect the user, they have to be authorized at runtime, so the user know if it's related to his action.

To do this :

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

It will show a DialogBox in which user will choose wether he autorize your app to use location or not.

Then get the user answer by using this function :

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

} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}

If the user accept it once, then your app will remember it and you won't need to send this DialogBox anymore. Note that the user could disable it later if he decided to. Then before requesting the location, you would have to test if the permission is still granted :

public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}

It's all explained on Android documentation (onRequestPermissionsResult from there too): http://developer.android.com/training/permissions/requesting.html

location provider requires ACCESS_FINE_LOCATION permission even if I use runtime request permission

Ok, I could fix the problem and I'll explain it here also in case someone else is facing a similar problem. I should add that while the problem is now fixed, it feels like a bug on Android or lack of documentation.

Because of some internal project reasons, I was requesting for the location permission like this:

PackageInfo packageInfo = pm.getPackageInfo(_activity.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null)
{
requestedPermissions = packageInfo.requestedPermissions;

if (requestedPermissions.length > 0)
{
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
_requestedPermissionsArrayList = new ArrayList<String>();
_requestedPermissionsArrayList.addAll(requestedPermissionsList);
}
}

for(int i=0; i < _requestedPermissionsArrayList.size(); i++)
{
if(_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_FINE_LOCATION) || // api level 1
_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_COARSE_LOCATION) // api level 1
)
{
isFound = true;
ActivityCompat.requestPermissions(_activity, new String[]{
_requestedPermissionsArrayList.get(i)
}, ExConsts.MY_PERMISSIONS_REQUEST);
break;
}
}

With this setup, the order of permissions in the manifest mattered! When I had:

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

it didn't work but when I switched their order, it did work.

Finally, how did I fix the problem? like below, I mentioned both permission names

ActivityCompat.requestPermissions(_activity, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}

Now, no matter the order of permissions in the manifest, it always worked fine. cheers.

Android “gps requires ACCESS_FINE_LOCATION” error

If you are targeting Android M, you need to ask users permission to access device GPS. Here is a not so clean code, but might help u a little bit. A better way to do it would be to create your own permission manager class to handle requests and a alertDialog ativity.

Whats happening below is

1) You are checking if permissions are granted.
2) If not, you are checking if the permissions have been denied previously, In that case, you are showing a rationale, to explain to the user why you need the permissions.
3) You show the request permission popup using ActivityCompat.
4) If user declines, show a snackbar with a view button to take the user to the app info screen, whenever u need to access the GPS but notice that permissions are not granted.

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
showRationale();
} else {
// do request the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 8);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ){
//Start your code
} else {
//Show snackbar
}
}
}

private void showRationale(){
String strDeleteMessage = getResources().getString(R.string.rationale_message11) ;

final View dialogView = LayoutInflater.from(this.getActivity()).inflate(R.layout.dialog_fragment, null);

final AlertDialog storageRationaleAlert = new AlertDialog.Builder(this.getActivity()).create();
storageRationaleAlert.setView(dialogView, 0, 0, 0, 0);
storageRationaleAlert.setCanceledOnTouchOutside(false);
TextView mDialogTitle = (TextView) dialogView.findViewById(R.id.dialog_title);
TextView mDialogDetails = (TextView) dialogView.findViewById(R.id.dialog_details);
mDialogDetails.setVisibility(View.VISIBLE);
Button mCancelButton = (Button) dialogView.findViewById(R.id.cancel_btn);
Button mOkButton = (Button) dialogView.findViewById(R.id.ok_btn);
mOkButton.setText(getString(R.string.dialog_continue));

mDialogDetails.setText(Html.fromHtml(strDeleteMessage));

final Activity activity = this.getActivity();
mOkButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
storageRationaleAlert.dismiss();

//Ask for GPS permission
}
});

mCancelButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
storageRationaleAlert.dismiss();
//Show permission snackbar
}
});

storageRationaleAlert.show();
}

I get: java.lang.SecurityException: gps location provider requires ACCESS_FINE_LOCATION permission

Update your code with bellow code may solve your problem

public class FragmentHome extends Fragment implements OnMapReadyCallback {

/*
Set up's
*/

private static final String TAG = "FragmentHome";

/*
Pallete
*/

private MapView mapView;
private GoogleMap gMap;

private static final String MAP_VIEW_BUNDLE_KEY = "AIzaSyDWL-JNHiCXvQefgFh1BdaAflJTveSrHJo";

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view_fragmentInflate = inflater.inflate(R.layout.fragment_fragment_home, container, false);

mapView = (MapView) view_fragmentInflate.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);

return view_fragmentInflate;
}

boolean isMapReady;
@Override
public void onMapReady(GoogleMap googleMap) {
isMapReady=true;
getUserLocation();
}

private void getUserLocation() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
return;
}
}

LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: " + location);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, locationListener);

}
}

Now override onRequestPermissionsResult() and check if result is granted and isMapReady flag true then call getUserLocation().

How to solve gps location provider requires ACCESS_FINE_LOCATION permission

I found that the problem is with setting to configuration of my localization pluggin before checking at the permission, soo simply I have moved it inside the if condition like:

 if (hasPermission) {
print('Location service has permission');
await _locationService.changeSettings(
accuracy: LocationAccuracy.high,
interval: 1000,
);
final location = await _locationService.getLocation();
currentLocation = LatLng(location.latitude, location.longitude);
_locationService.onLocationChanged.listen((data) =>
currentLocation = LatLng(data.latitude, data.longitude));
} else
throw geo.PermissionDeniedException(null);

java.lang.SecurityException gps location provider requires ACCESS_FINE_LOCATION permission

Make sense from logcat provided.
Add this in your manifest

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


Related Topics



Leave a reply



Submit