Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - NETWORK_PROVIDER not providing updated locations

LocationManager locationManager = (LocationManager)getApp().getSystemService(Context.LOCATION_SERVICE); //getApp() returns my Application object
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1, 1, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);

That's the code I'm using to listen. With GPS enabled, everything works fine. However, if I disable GPS and rely on network location, it gives me stale results -- in this case, from two days ago. I cannot get it to update. Calling getLastKnownLocation returns the same stale results.

Google Maps updates just fine, so I know it's not a hardware/system configuration problem.

I've Googled around, and found a few people with similar problems, but no answers. I've tried targeting my project to API level 8 (2.2) as well as 15 (4.0.3). Same results. My phone is running ICS.

I've also tried removing the request for GPS_PROVIDER, as well as including requests for everything in getAllProviders(). Same deal.

Any idea why NETWORK_PROVIDER is not updating? Any help would be greatly appreciated.

(P.S. No, I don't I normally use "1, 1" as my time/distance parameters; I just changed it while debugging this.)

EDIT: I forgot to mention that yes, isProviderEnabled() returns true.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally found a way to solve this problem (see my question here).

In my case on S3 Galaxy Mini there were similar symptoms (no location updates until reboot...) like the ones described above. Location in most cases stopped updating when the device reached low power conditions, but sometimes it just happened even when the phone was fully charged. Obviously this is a problem somewhere in the LocationManager. I managed to bypass LocationManager by using the new Google Play Services API for locations (LocationClient class).

This is probably the reason why the location updates were still working with Google Maps, because Google Maps were using Google Play Services API even before it was made public.

I recommend you to follow this link which shows the way how to use Play Services API to get locations instead of using LocationManager if you encounter this problem.

Im running location updates with Play Services and there were absolutely no problems like this anymore - I keep receiving regular location updates anytime.

Beware that this will require that the user must have Google Play installed on their device, so Kindles and the like will be out of the picture. It also requires a minimum of Android 2.2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...