Onlocationchanged() Never Called
Solution 1:
i run my app on real device . i use network instead of GPS and onLocationChanged is called:
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
Solution 2:
Do you never get a location or only as you write in your comment "sometimes it gets location but not at the time of click."?
Might be that your code is faster than LocationManager, which might not yet have called onLocationChanged(). Change your code in order to get the last known location, or wait until your locListener was called:
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,locListener);mobileLocation=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);if(mobileLocation!=null) {
Solution 3:
LocationManager.NETWORK_PROVIDER
need network, but it is real, not precise; if you want to use LocationManager.GPS_PROVIDER
, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you!
Solution 4:
I thing you forgot permission
<uses-permissionandroid:name="android.permission.ACCESS_GPS" /><uses-permissionandroid:name="android.permission.ACCESS_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.INTERNET" />
OR
Declare Proper Class like
publicclassMyLocationListenerimplementsLocationListener {
@OverridepublicvoidonLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
}
Solution 5:
Directly after locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener)
, add the following code:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locListener);
EDIT: corrected the code thanks to the comment by @Fabian Streitel.
Post a Comment for "Onlocationchanged() Never Called"