4485

Not able to get Location by GPS : Android

Question:

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

Answer1:

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?

Answer2:

locationManager.getLastKnownLocation will return null if the GPS has not had time to get a fix yet. Android does not provide a 'give me the location now' method. When the GPS has got a fix, the onLocationChanged() will run. It is <strong>event driven</strong>, you have to be patient and wait for that event. Once onLocationChanged() has run, then getLastKnownLocation() will return a non null value.

Answer3:

Try to replace the last line by:

List<String> providers = locationManager.getProviders(true); if(!providers.isEmpty()) { location = locationManager.getLastKnownLocation(providers.get(0)); } else { Log.d("ManualLog", "No provider"); }

Recommend

  • Little Fluffy Location Library can't find location
  • GPS location provider availability issue
  • Android detect location settings
  • Getting longitude and latitude values faster
  • isProviderEnabled(LocationManager.GPS_PROVIDER) always returns false in android 2.3
  • NullpointerException when calling getSystemService(Context.LOCATION_SERVICE) and onLocationChanged n
  • Android GPS Provider
  • NullpointerException when calling getSystemService(Context.LOCATION_SERVICE) and onLocationChanged n
  • Unable to get longitude and latitude in android
  • Android AsyncTask & LocationListener
  • Why extra data (integer) is not sent in android notification intent?
  • Picasso only loading one image in a BaseAdapter
  • BluetoothLeScanner could not find callback wrapper
  • Android DrawerLayout with MapFragment black screen when returning to map
  • Can't stop a service started with alarmManager
  • Android: Transfering Data via ContentIntent
  • Notification and AlarmManager
  • How to save state of CheckBox while scrolling in ListView?
  • Firebase messaging: default notification channel doesn't work
  • Why setOnCheckedChangeListener not working in a popup that uses LayoutInflater?
  • How to Filter ListAdapter using getFilter() within a fragment
  • ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in not firing
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • Push notifications don't work
  • AlarmManager is blocking main thread
  • Why is my AlarmManager firing off instantly?
  • onLeScan never gets called android
  • Missing permission to control media error on Android 5/L
  • How to get a value (ex: baseURL) in every Karate feature?
  • Perl system calls when running as another user using sudo
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?