onLocationChanged not called on some devices

旧街凉风 提交于 2019-11-30 06:28:41

I observed same behavior on Galaxy Nexus and recognized two things causing onLocationChanged() to not being called. There are

  1. Corresponding location source is not enabled in System Settings. For PRIORITY_HIGH_ACCURACY the GPS satellites must be enabled, for PRIORITY_BALANCED_POWER_ACCURACY Wi-Fi & mobile network location must be enabled.

  2. Even for the enabled location sources there can be no valid location information available. For instance, PRIORITY_HIGH_ACCURACY source is enabled, but there is no (or not good) GPS reception (e.g. device is inside a building). Or PRIORITY_BALANCED_POWER_ACCURACY source is enabled, but both Wi-Fi and mobile data are switched off. In those cases location cannot be detected and onLocationChanged() will not be called.

To fix #1 I had to check whether required location sources are enabled even before connecting to Google Services API and if they are switched off, then I notified a user asked him/her to allow location access.

To solve #2 I decided to use LocationRequest.setExpirationDuration(). I set timeout at about 10 seconds, and immediately after calling requestLocationUpdates() I post a callback delayed by same 10 seconds. If delayed callback is called before requestLocationUpdates(), this means location cannot be detected due to reason #2. I do three retries and then show user an error message.

Android documentation is not great at this place, that's why I hope this helps.

lr.setInterval(100); 

with it your interval is 0,1 second so it very hard for gps chipset to detect location. Your interval should be > 1 second.(with me it is > 10)

locationRequest it should be set:

setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) because PRIORITY_BALANCED_POWER_ACCURACY in outdoor it doesn't get location . Code will look like :

lr=LocationRequest.create();

lr.setInterval(5 * 1000);// 5s

lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationclient.requestLocationUpdates(lr, this);

Hope this help.

See if you're getting a passed location by testing for if (loc == null). Even though it's the same provider, maybe one device is coming back null and bombing out, resulting in a look like it's not firing.

This maybe non-sense but I have faced this problem before, someone suggested that you should create your own LocationListener instead of letting your MainActivity implements LocationListener. Basically this:

locationclient.requestLocationUpdates(lr, your_location_listener);

Hope this helps.

I solved this by setting both parameters, minimum distance and minimum time between updates to 0. As per what I have tested on 2 devices, now onLocationChanged() is being called regularly after requesting for updates.

try to remove and reinstall "google play services". Install it from google market link bellow, google play services

Here Check this code out...

public class MyActivity extends Activity implements LocationListener {
double destLat, destLong;

LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    destLat = location.getLatitude();
    destLong = location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

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