android Location Listener

限于喜欢 提交于 2019-12-21 20:18:34

问题


i want to listen to both GPS and NETWORK location provider from the same listener and implementation

is this ok for doing that:

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

will it user the same methods for both providers ?


回答1:


Google says here:

You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() twice—once for NETWORK_PROVIDER and once for GPS_PROVIDER.




回答2:


It's simple as 1.2.3 look at my example...

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

Location helper

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) {
    }
}

}




回答3:


Google provides a better API, using fused location API:

https://developers.google.com/location-context/fused-location-provider/

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

https://developer.android.com/training/location/receive-location-updates

https://github.com/sakurabird/Android-Fused-location-provider-example

https://youtu.be/eHjHlujp3Tg?list=WL&t=2129



来源:https://stackoverflow.com/questions/12218075/android-location-listener

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