Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;
import android.content.Context;

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

public class Main {
    /**
     * Returns current (last known) location of the system. If the context is missing permissions
     * (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION) or location tracking is disabled by user, this
     * will fail and return null.
     * 
     * @param context
     *            Application context of caller
     * @return Current location or null if we could not retrieve current location
     */
    public static Location getCurrentLocation(Context context) {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = lm.getProviders(true);

        // Find most accurate last known location
        Location location = null;
        for (int i = providers.size() - 1; i >= 0; i--) {
            location = lm.getLastKnownLocation(providers.get(i));
            if (location != null)
                break;
        }

        return location;
    }
}