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);
}