Checks whether both GPS and network location is enabled on the device - Android Map

Android examples for Map:GPS

Description

Checks whether both GPS and network location is enabled on the device

Demo Code


//package com.java2s;
import android.content.Context;
import android.location.LocationManager;

public class Main {
    /**//ww w  . j ava  2 s  .c  o  m
     * Checks whether both GPS and network location is enabled on the device
     * @param context The context to check with
     * @return True if both providers are enabled
     */
    public static boolean isHighAccuracyEnabled(Context context) {
        return isGpsProviderEnabled(context)
                && isNetworkProviderEnabled(context);
    }

    /**
     * Checks whether GPS is enabled on the device
     * @param context The context to check with
     * @return True if GPS is enabled
     */
    public static boolean isGpsProviderEnabled(Context context) {
        if (context == null) {
            return false;
        }
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        return locationManager != null
                && locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /**
     * Checks whether network location is enabled on the device
     * @param context The context to check with
     * @return True if network location is enabled
     */
    public static boolean isNetworkProviderEnabled(Context context) {
        if (context == null) {
            return false;
        }
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        return locationManager != null
                && locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }
}

Related Tutorials