7

The following is my AndroidManifest.xml...

<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" />

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="22" />

<application
  android:name=".BeaconApp"
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyBqhOHH31FYObdzoVW9TmQsv62TOP0LSLI"/>
</application>

As you can see I added the proper permissions necessary to get location info. However, when I run my app, I get a strange error...

Looks like the app doesn't have permission to access location.
Add the following line to your app's AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

throwLocationPermissionMissing
...etc

I already added this in my AndroidManifest.xml so why am I getting this error. I tried restarting Android Studio. I tried Wiping Data from emulator. I tried Gradle -> Clean. None of these helped. What is going on here?

Is it possible that my android emulator is blocking GPS or something?

buydadip
  • 8,890
  • 22
  • 79
  • 154

3 Answers3

9

Have you tried asking permissions at runtime..

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.ACCESS_FINE_LOCATION);
if(permissionCheck != PackageManager.PERMISSION_GRANTED) {
    // ask permissions here using below code
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            REQUEST_CODE);
}

You can check details on this site Requesting Permissions at Run Time

This is the way you ask user to grant permission at runtime after android 6.0

You can check Android 6.0 Permission Error answer also.

Hope it'll help you.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29
  • I see so I put this code in my `MainActivity`? I'm currently using react-native, not sure if that matters – buydadip Dec 04 '16 at 03:50
  • Yes, you have to put this code in your Activity where you are accessing location. Add this code in `onCreate` method – ELITE Dec 04 '16 at 03:51
  • also note that you have to check if user granted permission or not by overriding `onRequestPermissionsResult` method – ELITE Dec 04 '16 at 03:52
  • Hello mr@ELITE I am Using react-native Getting Same Error.. Am I use it this ..If am Use it Were iam placed it... can u plz suggest me – Lavaraju Apr 29 '17 at 11:54
  • what is `react-native`?? If you're using native android code, you have to add this code in your `MainActivity` before using user's location. – ELITE Apr 30 '17 at 15:20
  • what is the value that would be assigned for `REQUEST_CODE` – Ghada Nov 04 '17 at 18:23
  • you can assign any value..in my case it was `102`. – ELITE Nov 04 '17 at 18:35
5

what you need is the runtime permission. technically speaking, since os 6, the manifest permission doesnt mean anything.

google "android runtime gps permission" and you'll get tons of answers on what to do and why.

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
4

With React-Native you have to use PermissionsAndroid to get permissions on Android 6.0 (or later) devices..

Add these lines to your files

import { PermissionsAndroid } from 'react-native';

async function requestLocationPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        'title': 'Location Permission',
        'message': 'This app needs access to your location',
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the location")
    } else {
      console.log("Location permission denied")
    }
  } catch (err) {
    console.warn(err)
  }
}

then call requestLocationPermission() before using the location

MujtabaFR
  • 5,956
  • 6
  • 40
  • 66