Code example for LocationManager: Finding current location

Methods: getLastKnownLocationrequestLocationUpdates, getClass

0
        } 
        Settings.System.putString(getContentResolver(),
            Settings.System.LOCATION_PROVIDERS_ALLOWED, allowedLocationProviders);
        try { 
            Method m =
                mLocationManager.getClass().getMethod("updateProviders", new Class[] {});
            m.setAccessible(true);
            m.invoke(mLocationManager, new Object[]{});
        } 
        catch(Exception e) {
        } 
        return; 
    } 
 
    /** 
     * Sends the location to the user. 
     * @param location the location to send. 
     */ 
    public void sendLocationUpdate(Location location) {
        StringBuilder builder = new StringBuilder();
        builder.append("http://maps.google.com/maps?q=" + location.getLatitude() + "," + location.getLongitude() + " (");
        builder.append("accuracy: " + location.getAccuracy() + "m ");
        builder.append("altitude: " + location.getAltitude() + " ");
        builder.append("speed: " + location.getSpeed() + "m/s ");
        builder.append("provider: " + location.getProvider() + ")");
        XmppService service = XmppService.getInstance();
        if (service != null) {
            service.send(builder.toString());
        } 
    } 
 
    public void onStart(final Intent intent, int startId) {
        super.onStart(intent, startId);
 
        if ( intent.getAction().equals(STOP_SERVICE) ) {
            destroy(); 
            stopSelf();
            return; 
        } 
 
        try { 
            if(!getGPSStatus()) 
                setGPSStatus(true); 
        } 
        catch(Exception e) {
        } 
        mLocationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation = location;
                    sendLocationUpdate(currentBestLocation);
                } 
            } 
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
            public void onProviderDisabled(String arg0) {}
            public void onProviderEnabled(String arg0) {}
        }; 
 
        // We query every available location providers 
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
 
        Location location = mLocationManager.getLastKnownLocation("gps");
        if (location == null)
        { 
            location = mLocationManager.getLastKnownLocation("network");
            if (location != null) {
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation = location;
                    XmppService service = XmppService.getInstance();
                    if (service != null) {