get Last Known Location From Best Available Provider - Android Map

Android examples for Map:Location Provider

Description

get Last Known Location From Best Available Provider

Demo Code


//package com.java2s;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class Main {
    public static Location getLastKnownLocationFromBestAvailableProvider(
            Context mContext) {/*w  w w  . j av  a  2 s  . com*/
        LocationManager locationManager = (LocationManager) mContext
                .getSystemService(Context.LOCATION_SERVICE);

        if (locationManager == null)
            return null;

        Location fromNetwork = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location fromGps = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        return fromGps != null ? fromGps : fromNetwork;
    }
}

Related Tutorials