Example usage for android.location LocationManager GPS_PROVIDER

List of usage examples for android.location LocationManager GPS_PROVIDER

Introduction

In this page you can find the example usage for android.location LocationManager GPS_PROVIDER.

Prototype

String GPS_PROVIDER

To view the source code for android.location LocationManager GPS_PROVIDER.

Click Source Link

Document

Name of the GPS location provider.

Usage

From source file:Main.java

/**
 * This method checks phone if it is able to get user's location
 * @param context/* ww  w . j  ava  2 s  . co m*/
 * @return
 */
public static boolean testLocationServices(Context context) {

    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location locationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    boolean haveGPS = (locationGPS != null);
    boolean haveNet = (locationNet != null);

    return (haveGPS || haveNet);
}

From source file:Main.java

public static void isGPSActivated(final Context context) {
    LocationManager lm = null;/*from  w w  w  .  j  a va  2s  . co  m*/
    boolean gpsEnabled = false;
    if (lm == null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    try {
        gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }

    if (!gpsEnabled) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);

        dialog.setCancelable(false);
        dialog.show();

    }
}

From source file:Main.java

public static boolean isGpsEnabled(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

From source file:Main.java

/**
 * This Function check the GPS enable/disable Status. Remember to add
 * required Permission in Manifest.//from  w  w  w .j  a  va2s  .  c  o  m
 * 
 * See below for @params.
 */

public static boolean isGPSEnable(Context ctx) {
    LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

From source file:Main.java

/**
 * Tells whether location is enabled.//from   w  w w.j  av  a 2 s  .co m
 *
 * @param context a reference to the context.
 * @return true if location is enabled, false otherwise.
 */
public static boolean isLocationEnabled(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    return isGpsEnabled | lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

From source file:Main.java

public static void openGPS(Context context) {
    Log.d(TAG, "openGPS");
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
        Intent gpsIntent = new Intent();
        gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
        gpsIntent.setData(Uri.parse("custom:3"));
        try {/* w  w w  .ja va2 s  .co  m*/
            PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();

        } catch (CanceledException e) {
            Log.d(TAG, "openGPS exception:" + e.getMessage());

            e.printStackTrace();
        }

    }
}

From source file:Main.java

public static String getGpsEnabled(Context context) {
    if (PackageManager.PERMISSION_GRANTED == context
            .checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
        final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return Boolean.toString(manager.isProviderEnabled(LocationManager.GPS_PROVIDER));
    } else {// w w  w  . j  ava  2s.c o  m
        return null;
    }
}

From source file:Main.java

/**
 * Check GPS provider/*from w w  w  .j av a2s  .c o m*/
 * 
 * @param context
 * @return true if enable or false if not
 */
public static boolean isGPSProviderEnabled(Context context) {
    LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    boolean value = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!value) {
        value = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    return value;
}

From source file:Main.java

/**
 * This method returns the last,best known current location for user
 * @param context/*from w w  w  . j  ava2  s . c o m*/
 * @return
 */
public static Location getLastBestLocation(Context context) {

    // get location providers
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location locationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    // check which ones are available
    boolean haveGPS = (locationGPS != null);
    boolean haveNet = (locationNet != null);

    if (!haveGPS && !haveNet) { // no providers available
        String msg = "Could not find a location. Using random location now.";
        displayAlert(context, msg);
        Location l = new Location("yum fake location");
        l.setLatitude(45.0);
        l.setLongitude(45.0);
        // uh oh... no location
        return new Location(l);

    } else if (haveGPS && !haveNet) { // only gps available

        return locationGPS;

    } else if (!haveGPS && haveNet) { // only cell location available

        return locationNet;

    } else { // choose the most recent one

        long GPSLocationTime = locationGPS.getTime();
        long NetLocationTime = locationNet.getTime();

        if (GPSLocationTime - NetLocationTime > 0) {

            return locationGPS;

        } else {

            return locationNet;

        }
    }

}

From source file:Main.java

/**
 * 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
 *//*from   www . j  a  va 2 s.  co  m*/
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);
}