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.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

public class Main {
    public static String getLocality(Context context) {
        @SuppressWarnings("static-access")
        LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
        if (lm == null)
            return "";
        Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (l == null)
            return "";
        Geocoder gc = new Geocoder(context);

        try {
            List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
            if (address != null && address.isEmpty() == false)
                return address.get(0).getLocality();
        } catch (Exception e) {
            return "";
        }

        return "";
    }
}