LocationManager requestLocationUpdates doesn't work

雨燕双飞 提交于 2019-12-05 11:07:55

You can also let your phone determine the best provider instead of just setting

String locationProvider = LocationManager.NETWORK_PROVIDER;

Try this instead:

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);   
String locationProvider = mlocManager.getBestProvider(criteria, true);

Also, have you tested by moving your device to a new location? Otherwise the "onlocationchanged" event may not fire. When I was figuring this stuff out a couple years ago, I put in some "toast" to alert me of my lat/long coordinates as I walked around the block to verify the event was firing.

This looks like Android Bug 57707 to me.

If it is, rebooting the device should help, at least for a while.

Or you could use the Google Play Services Location API.

By using both Wifi, GPS and Network positioning, the Google Play Services Location API will hide the fact that Network positioning is broken; that will only be obvious when you have neither GPS nor Wifi available.

Make sure that the proper location services are enabled in Settings. You can also use isProviderEnabled() to check if the particular provider you've chosen will work. And ensure you have the COARSE or FINE location permissions in your manifest.

Also, make sure you're testing on a device. Network location does not work in the emulator at all, and GPS will only work if you use the simulation tool to inject new data points.

I have faced a similar problem. Try replacing getApplication() with MainAvtivity.this

Also, make sure you are keeping permissions for both FINE and COARSE locations. If your target API is more than 23, you should handle permissions from java code also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!