android位置侦听器
我想从同一个侦听器和实现侦听GPS和网络位置提供程序 这样做可以吗:android位置侦听器,android,gps,location,android-loadermanager,Android,Gps,Location,Android Loadermanager,我想从同一个侦听器和实现侦听GPS和网络位置提供程序 这样做可以吗: locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this); 它会为两个提供商使用
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this);
它会为两个提供商使用相同的方法吗?谷歌表示:
您还可以通过两次调用requestLocationUpdates()从GPS和网络位置提供程序请求位置更新,一次用于网络位置提供程序,一次用于GPS位置提供程序
简单到1.2.3看看我的例子
try {
Criteria criteria = new Criteria();
mLocationManagerHelper.SetLocationManager((LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE));
mLocationManagerHelper.GetLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, mLocationManagerHelper.GetLocationListener());
String provider = mLocationManagerHelper.GetLocationManager().getBestProvider(criteria, false);
Location location = mLocationManagerHelper.GetLocationManager().getLastKnownLocation(provider);
if (location != null) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
} catch (Exception ex) {
Log.e(TAG, "GPS", ex);
}
位置助手
public class LocationManagerHelper {
private static final String TAG = LocationManagerHelper.class.getSimpleName();
private Context mContext;
private LocationManager mLocationManager;
private GeoUpdateHandler mLocationListener = new GeoUpdateHandler();
public LocationManagerHelper(Context context) {
this.mContext = context;
}
public GeoUpdateHandler GetLocationListener() {
return mLocationListener;
}
public void SetLocationManager(LocationManager locationManager) {
mLocationManager = locationManager;
}
public LocationManager GetLocationManager() {
return mLocationManager;
}
public void Stop() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
}
private class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
String longitude = "Longitude: " + loc.getLongitude();
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}谷歌使用融合的位置API提供了更好的API:
如果您在询问之前先阅读文档,那就太好了。是的,你可以,但是你需要管理提供者的准确性,你如何做到这一点当然取决于你。另外,设置提供者以尽可能快地发送更新将消耗大量的功率,因此您可能需要考虑是否真的需要。还请检查@ LDISTIC“请求来自GPS提供商的位置更新,替NETWorkLoad提供GPSL提供程序”。“DeNoTrNeNice回答中的行,确实非常有用。您问:使用它可以吗?”它会对两个提供者使用相同的方法吗谷歌回答:是的,你可以这样做,他们将为两个提供商提供相同的功能!你说得对,对不起:)我从手机上读到的,似乎你建议我这么做。谢谢您在哪里根据@Asaf Nevo requirementwooops为GPS和网络位置提供商实现了侦听器?代码不对,您只需添加另一个侦听器,然后您可以自己定制,然后按照我向您展示的方式使用。