3

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 ?

rajpara
  • 5,203
  • 1
  • 29
  • 46
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154
  • 1
    it will be great if you read the document first before asking here. http://developer.android.com/guide/topics/location/strategies.html – rajpara Aug 31 '12 at 15:10
  • Yes you can, but you will need to manage the accuracy of the providers, how you do that is up to you of course. Also setting the provider to send updates as fast as possible will consume a lot of power so you might want to consider if you really need that. – Idistic Aug 31 '12 at 15:10
  • Also check [this tutorial out](http://www.anddev.org/novice-tutorials-f8/gps-and-network-location-t16563.html) – jnthnjns Aug 31 '12 at 15:11
  • @ldistic "To request location updates from the GPS provider, substitute GPS_PROVIDER for NETWORK_PROVIDER." line from document – rajpara Aug 31 '12 at 15:12

3 Answers3

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.

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
1

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

}

hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • where did you implemented listener for both GPS and NETWORK location provider as per @Asaf-Nevo requirement – rajpara Aug 31 '12 at 15:36
  • 1
    wooops not right code, any way you just have to add another listener, your own custom after that you just use the as usual the same way i have showed you. – hackp0int Aug 31 '12 at 15:39