get Last Known Location In Application - Android Map

Android examples for Map:Last Location

Description

get Last Known Location In Application

Demo Code


//package com.java2s;

import android.content.Context;
import android.location.Location;

import android.location.LocationManager;

import android.util.Log;

public class Main {
    public static final String TAG = "DSN Debug";
    public static final String PROVIDER_NAME = "testProvider";

    public static Location getLastKnownLocationInApplication(Context ctx) {
        LocationManager lm = (LocationManager) ctx
                .getSystemService(Context.LOCATION_SERVICE);
        return getLastKnownLocationInApplication(ctx, lm, PROVIDER_NAME);
    }//from  w  w w.ja v a 2 s.co  m

    public static Location getLastKnownLocationInApplication(Context ctx,
            LocationManager manager, String provider) {
        Location testLoc = null;

        if (manager.getAllProviders().contains(provider)) {
            testLoc = manager.getLastKnownLocation(provider);
            Log.d(TAG, "TestLocation: " + testLoc);
        }

        if (testLoc != null) {
            return testLoc;
        } else {
            return manager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
    }
}

Related Tutorials