get Location from LocationManager by Criteria - Android Map

Android examples for Map:Location

Description

get Location from LocationManager by Criteria

Demo Code


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

public class Main {
    static LocationManager locationManager;

    public static void getLocation(Context context,
            LocationListener listener) {

        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) context
                .getSystemService(serviceName);
        Criteria criteria = new Criteria();

        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);//from   ww  w  .  j a va 2  s  .  c  o  m
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 6000, 10, listener);

    }
}

Related Tutorials