[Android-development] “gps requires ACCESS_FINE_LOCATION” error and workaround on Android API 23+

Luke Dupin lukedupin at gmail.com
Fri Feb 12 23:32:13 CET 2016


As of Android API 23, there is a new way to request location permissions.
It doesn't look like QT5.6 beta has been updated to use this method, so I
wrote a quick work around. This API 23 change might affect the camera and
contacts permissions also, I'm not sure.

Before anyone says, I already have the fine location permission in my
manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Inside your android project, create a template. In the manifest, change:
android:name="org.qtproject.qt5.android.bindings.QtActivity"
to:
android:name="com.my_company.my_project.MyActivity"

Create the path inside your android template directory:
src/com/my_company/my_project

Now create the extended activity which requests the permissions as follows:

public class MyActivity
  extends QtActivity{
  private static final String[] LOCATION_PERMS={
      Manifest.permission.ACCESS_FINE_LOCATION
  };

  private static final int LOCATION_REQUEST = 1337;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

      //Ensure we can run the app
    if ( !canAccessLocation() )
      requestPermissions( LOCATION_PERMS, LOCATION_REQUEST );
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults)
  {
      //Figure out what was given to us
    switch(requestCode)
    {
      case LOCATION_REQUEST:
        if ( canAccessLocation() )
          log("Access to location is granted");
        else
          log("Access to location is denied");
        break;
    }
  }

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

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

  public static void log( String s )
  {
    Log.d("Radius", s );
  }
}

This "fix" was taken from the following post:
http://stackoverflow.com/questions/32083913/android-gps-requires-access-fine-location-error-even-though-my-manifest-file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/android-development/attachments/20160212/bfee3e41/attachment.html>


More information about the Android-development mailing list