How to read location only once with locationManager (GPS and NETWORK PROVIDER) and not any more looking for updates of location?

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

How to read location only once with locationManager (GPS and NETWORK PROVIDER) and not any more looking for updates of location, to save battery?

回答1:

Although requestSingleUpdate() is the technically correct answer, you should use

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener); 

Wait for a while after getting your first location. The location tends to wobble for a few seconds. To determine if the fix is stable use, location.getAccuracy(). Once the accuracy stabilizes, call locationManager.removeUpdates(mLocationListener);



回答2:

public LocationManager locationManager;  try {   locationManager.requestSingleUpdate( LocationManager.GPS_PROVIDER, new MyLocationListenerGPS(), null );   } catch ( SecurityException e ) { e.printStackTrace(); }  public class MyLocationListenerGPS implements LocationListener {  @Override  public void onLocationChanged(Location location) {  // your code and required methods go here... } 

Using 'null' in 'requestSingleUpdate' keeps the update check on the same thread (for use in a service). The method allows for you to put it in a Looper if you're running it from an activity. I would think that using an async task would be the better approach.



回答3:

Just remove updated from the location manager.

 lmanager.removeUpdates(this); 


回答4:

To get the location only once you can refer the below link

http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(android.location.Criteria,android.location.LocationListener,android.os.Looper)

and search for the "requestSingleUpdate"



回答5:

Please read Obtaining User Location developer guide : http://developer.android.com/guide/topics/location/obtaining-user-location.html

This link for best Performance

Don't forget to specify android.permission.ACCESS_FINE_LOCATION in the Android manifest



回答6:

Its easy just once read the loacation update like

locationManager.requestLocationUpdates(provider, 400, 1, this);

and after reading the location once called

locationManager.removeUpdates(this);

so system will not ask for location update after this and you cab save your battery.



回答7:

Using requestSingleUpdate with your LocationManager. For more info, official guide is interesting: INFO



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