Gps On App only works after Using Google Maps

时光毁灭记忆、已成空白 提交于 2020-01-04 04:15:12

问题


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);


来源:https://stackoverflow.com/questions/12646477/gps-on-app-only-works-after-using-google-maps

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