android getlastknownlocation always null in emulator

后端 未结 3 1179
温柔的废话
温柔的废话 2021-01-06 19:24

It seems a common issue, but i really can\'t understand why is it happening even though i read ton of simialr issues.

I am playing with a basic location class on a e

相关标签:
3条回答
  • 2021-01-06 20:00

    getLastKnownLocation() will return null in general, if no app has requested a location fix from your desired provider recently.

    On the emulator, it seems to take it one step further: you do not get a location unless one has been delivered to the emulator since your app has called requestLocationUpdates().

    The general pattern for using getLastKnownLocation(), if you want decent odds of getting an actual Location, is to use requestLocationUpdates() and removeLocationUpdates() to ensure that LocationManager is actively seeking locations via your chosen provider. Of course, you could also use LocationListener and onLocationChanged(), rather than getLastKnownLocation(), if you so chose.

    0 讨论(0)
  • 2021-01-06 20:11

    There is another option, to create your own provider using locationManager.addTestProvider(); so you can make your own tests.

    0 讨论(0)
  • 2021-01-06 20:15

    @CommonsWare's answer is clear. To get not-null on emulator go to Google Maps application and after that you get some location

    0 讨论(0)
提交回复
热议问题