Android location manager permissions to be used

Brahadeesh picture Brahadeesh · Aug 1, 2011 · Viewed 32.4k times · Source

I have the following code in my android project:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location currentLocation = locationManager.getLastKnownLocation(bestProvider);
location = currentLocation.getLatitude() + " " + currentLocation.getLongitude();
MyLocation.setText(location);

I am getting an provider == null error. what permissions do I need to use?

My android manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.DuckTag"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".DuckTagActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
</manifest>

Thank you

Answer

Kurru picture Kurru · Aug 1, 2011

Here is what you need to add to your manifest file

GPS-based location

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

Network-based location

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

Previous posters are wrong, if you want to use both, then only ACCESS_FINE_LOCATION is required, as detailed here: http://developer.android.com/guide/topics/location/obtaining-user-location.html

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.