Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
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 getCountry(Context context) {
        @SuppressWarnings("static-access")
        LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
        Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        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).getCountryName();
        } catch (IOException e) {
            return "";
        }

        return "";
    }
}