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.LocationManager;

public class Main {
    /**
     * Returns whether location services are currently active in the specified context. Location
     * services are active if both GPS and network location services are enabled.
     * @param context the context from which to check
     * @return true if there location services are on, otherwise false
     */
    public static boolean isLocationServicesOn(Context context) {
        LocationManager lm = (LocationManager) context.getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
        boolean gpsEnabled = false;
        boolean networkEnabled = false;

        // check whether GPS and network providers are enabled
        try {
            gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception e) {
        }

        try {
            networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
        }

        // only show dialog if location services are not enabled
        return (gpsEnabled || networkEnabled);
    }
}