9

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

jonalmeida
  • 1,206
  • 1
  • 9
  • 24
Brahadeesh
  • 2,245
  • 8
  • 40
  • 58

3 Answers3

38

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.

Kurru
  • 14,180
  • 18
  • 64
  • 84
18

You need this

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
PravinCG
  • 7,688
  • 3
  • 30
  • 55
  • 2
    Just for reference, you don't actually need both these permissions unless you use both (which the OP does in this case). See the developer [reference](http://developer.android.com/reference/android/Manifest.permission.html) for more information. – thegrinner Aug 01 '11 at 14:40
  • 7
    You _don't_ need both Permissions. ACCESS_FINE_LOCATION includes ACCESS_COARSE_LOCATION. – basti Oct 23 '13 at 13:55
2

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

Snicolas
  • 37,840
  • 15
  • 114
  • 173