Code example for LocationManager: Finding current location

Methods: getLastKnownLocationisProviderEnabledrequestLocationUpdates

0
     * @param myLocationFoundListener the callback 
     */ 
    public void requestLocationUpdates(MyLocationFoundListener myLocationFoundListener) {
        mMyLocationFoundListener = myLocationFoundListener;
 
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mGpsLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    0, 0, mGpsLocationListener);
        } 
 
        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            mNetworkLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    0, 0, mNetworkLocationListener);
        } 
 
        if (mNetworkLocationListener != null || mGpsLocationListener != null) {
            mLocationRequestTimeOut.start();
        } else { 
            reportLocationFound(getLastKnownLocation()); 
        } 
    } 
 
    /** 
     * Removes any current registration for location updates.  
     */ 
    public void removeUpdates() { 
        if (mGpsLocationListener != null)
            mLocationManager.removeUpdates(mGpsLocationListener);
        if (mNetworkLocationListener != null)
            mLocationManager.removeUpdates(mNetworkLocationListener);
 
        mLocationRequestTimeOut.cancel();
        mMyLocationFoundListener = null;
    } 
 
    /** 
     * Get the last known location, will look for the best location from both 
     * GPS and the network provider. 
     * @return 
     */ 
    public Location getLastKnownLocation() {
        Location gpsLocation =
            mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location networkLocation =
            mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 
        return getBestLocation(gpsLocation, networkLocation);
    }