Brahadeesh
Brahadeesh

Reputation: 2255

Android location manager permissions to be used

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

Upvotes: 9

Views: 33373

Answers (3)

Kurru
Kurru

Reputation: 14321

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.

Upvotes: 38

Snicolas
Snicolas

Reputation: 38168

Here is what you need to add to your manifest file :

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

for GPS-based location or

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

for network-based location

You can require both if you want to build a more versatile application.

Greetings,
Stéphane

Upvotes: 2

PravinCG
PravinCG

Reputation: 7708

You need this

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

Upvotes: 18

Related Questions