I am new to android. I am trying to get my location using GPS.
I have included the permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
Whenever I am trying to get my Location from LocationManager using getLastKnownLocation I am getting null object. Also, GPS is enabled. Here is my code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location location;
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("ManualLog", "GPS_DISABLED");
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(settingsIntent);
} else {
Log.d("ManualLog", "GPS_ENABLED");
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
Are you running it on emulator or device?
If on emulator, you may need to send GPS data manually to emulator via “emulator control”. In eclipse, you can find it Window>Open Perspective>DDMS.
If you are running on device, is it possible your device never received a GPS signal before?