Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

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

public class Main {
    public static String getGpsString(Context context) {
        if (context == null) {
            return null;
        }

        String gps = null;

        Location location = null;
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);

        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(criteria, true);
        if (provider != null) {
            location = locationManager.getLastKnownLocation(provider);
        }
        if (location != null) {
            double longtitude = location.getLongitude();
            double latitude = location.getLatitude();
            gps = "Lon:" + longtitude + "; Lat:" + latitude;
        }

        if (gps == null) {
            gps = "Lon:0; Lat:0";
        }
        return gps;
    }
}