Gps On App only works after Using Google Maps

2019-07-04 00:33发布

问题:

We have this android app that uses the maps api. Now when we run it, it usually doesn't work, when we call

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
100, 0, this, null);

It doesn't really work, the LocationListener's onLocationChanged method is never called. However, if we run Google Maps and wait until the gps icon is on in the status bar, then our app does start calling onLocationChanged. So my question is, how is Google Maps "activating" gps, or if they're not activating it, what are they doing?

回答1:

You need to turn on GPS first:

try {
  Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
} catch (Exception e) {
  logger.log(Log.ERROR, e, e.getMessage());
}

To do that, you also need to have permission from end-user i believe.



回答2:

Try this:- we have to register location listener with the locationManager.onLocationChanged method will be execute, when we provide locationlitener

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
 100, 0, this, locationlistener);