Android Context Get getLastKnownLocation(Context context)

Here you can find the source of getLastKnownLocation(Context context)

Description

get Last Known Location

Declaration

public static Location getLastKnownLocation(Context context) 

Method Source Code

//package com.java2s;

import android.content.Context;

import android.content.SharedPreferences;

import android.location.Location;
import android.location.LocationManager;

import android.preference.PreferenceManager;

import java.util.List;

public class Main {
    public static final String LOCATION_IS_LOATION_AVA = "isLocationAvailable";

    public static Location getLastKnownLocation(Context context) {
        LocationManager lm = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = lm.getProviders(true);

        SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor settingsEditor = settings.edit();

        // Loop over the array backwards (more accurate)
        // if you get an accurate location, then break out the loop
        Location loc = null;/*from  w  w w . j  a v a  2 s. co  m*/
        for (int i = providers.size() - 1; i >= 0; i--) {
            loc = lm.getLastKnownLocation(providers.get(i));
            if (loc != null)
                break;
        }

        // Since the user disabled GPS, we assume they are in downtown Toronto
        if (loc == null) {
            settingsEditor.putBoolean(LOCATION_IS_LOATION_AVA, false);

            loc = new Location("Mock Location");
            loc.setLatitude(43.6481);
            loc.setLongitude(-79.4042);
        } else {
            settingsEditor.putBoolean(LOCATION_IS_LOATION_AVA, true);
        }

        settingsEditor.commit();

        return loc;
    }
}

Related

  1. getHelpUrl(Context context, String s)
  2. getImei(Context context, String imei)
  3. getInstalleApps(Context mContext)
  4. getInstance(Context context)
  5. getIntSPR(String key, Context context)
  6. getLineErrorPaint(Context context)
  7. getLinePaint(Context context)
  8. getLineThickness(Context context)
  9. getLocationProvider(Context context)