【问题标题】:android Location Listener安卓位置监听器
【发布时间】:2012-08-31 15:07:38
【问题描述】:

我想从同一个监听器和实现同时收听 GPS 和 NETWORK 位置提供程序

这样做可以吗:

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this);

它会为两个提供者使用相同的方法吗?

【问题讨论】:

  • 如果您在问这里之前先阅读文档会很棒。 developer.android.com/guide/topics/location/strategies.html
  • 是的,您可以,但您需要管理提供者的准确性,您如何做到这一点当然取决于您。此外,将提供程序设置为尽快发送更新会消耗大量电量,因此您可能需要考虑是否真的需要。
  • 同时检查this tutorial out
  • @ldistic "要向 GPS 提供商请求位置更新,请将 GPS_PROVIDER 替换为 NETWORK_PROVIDER。"文档中的行

标签: android gps location android-loadermanager


【解决方案1】:

谷歌说here

您还可以通过调用 requestLocationUpdates() 两次从 GPS 和网络位置提供程序请求位置更新——一次用于 NETWORK_PROVIDER,一次用于 GPS_PROVIDER。

【讨论】:

  • 不错的答案,确实很有帮助。
  • 你问:用那个可以吗?它会为两个提供商使用相同的方法吗? ||谷歌回答:是的,你可以这样做,他们将为两个提供商提供相同的功能!
  • 你说得对,对不起 :) 我已经从手机上阅读了它,看起来就像你建议我做的那样。谢谢!
【解决方案2】:

简单到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) {
    }
}

}

【讨论】:

  • 根据@Asaf-Nevo 要求,您在哪里为 GPS 和 NETWORK 位置提供程序实现了监听器
  • 哎呀代码不正确,无论如何你只需要添加另一个监听器,你自己的自定义之后你只需像往常一样使用我向你展示的方式。
【解决方案3】:
猜你喜欢
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多