安卓位置监听器

3

我想从同一个监听器和实现中同时获取GPS和网络位置提供程序的信息。

以下是实现该功能的代码,您可以参考:

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

它会使用相同的方法来处理这两个提供程序吗?

1
请阅读以下文档,了解有关Android定位策略的内容:http://developer.android.com/guide/topics/location/strategies.html - rajpara
是的,你可以这样做,但你需要管理提供者的准确性,如何做取决于你。另外,将提供者设置为尽可能快地发送更新会消耗大量电力,因此你可能需要考虑是否真的需要这样做。 - Idistic
还可以查看这个教程 - jnthnjns
从文档中提取的@ldistic "要从GPS提供程序请求位置更新,请将NETWORK_PROVIDER替换为GPS_PROVIDER。"行。 - rajpara
3个回答

1

这很简单,就像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位置提供程序的监听器? - rajpara
1
糟糕,代码不对。无论如何,您只需要添加另一个监听器,自定义您自己的监听器,然后像我展示的那样使用它就可以了。 - hackp0int

1

谷歌在这里说:

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


1
很好的答案,确实非常有帮助。 - rajpara
你问道:使用那个可以吗?对于两个提供商,它们会使用相同的方法吗?|| 谷歌回答道:是的,你可以这样做,它们将为两个提供商执行相同的功能! - Hesham Saeed
你说得对,抱歉 :) 我是在手机上阅读的,所以看起来就像你建议我做的那样。谢谢! - Asaf Nevo


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接