Example usage for android.content Context LOCATION_SERVICE

List of usage examples for android.content Context LOCATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context LOCATION_SERVICE.

Prototype

String LOCATION_SERVICE

To view the source code for android.content Context LOCATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.location.LocationManager for controlling location updates.

Usage

From source file:Main.java

/**
 * Check GPS provider//from w  w  w .  jav  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

/**
 * Checks if the current device has a  NETWORK module (hardware)
 * @return true if the current device has NETWORK
 *//*w w w  . j a  v a 2 s . com*/
public static boolean hasNetworkModule(final Context context) {
    final LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    for (final String provider : locationManager.getAllProviders()) {
        if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
            return true;
        }
    }
    return false;
}

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./*w  ww.  j a v  a 2 s  . c om*/
 * 
 * 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

public static android.location.Location getLastBestLocation(Context _context) {
    if (locManager == null)
        locManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);

    int minDistance = (int) 500;
    long minTime = System.currentTimeMillis() - (900 * 1000);

    android.location.Location bestResult = null;

    float bestAccuracy = Float.MAX_VALUE;
    long bestTime = Long.MIN_VALUE;

    // Iterate through all the providers on the system, keeping
    // note of the most accurate result within the acceptable time limit.
    // If no result is found within maxTime, return the newest Location.
    List<String> matchingProviders = locManager.getAllProviders();
    for (String provider : matchingProviders) {
        android.location.Location location = locManager.getLastKnownLocation(provider);
        if (location != null) {
            //                log(TAG, " location: " + location.getLatitude() + "," + location.getLongitude() + "," + location.getAccuracy() + "," + location.getSpeed() + "m/s");
            float accuracy = location.getAccuracy();
            long time = location.getTime();
            //                log(TAG, "time>minTime: " + (time > minTime) + ", accuracy<bestAccuracy: " + (accuracy < bestAccuracy));
            //                if ((time > minTime && accuracy < bestAccuracy)) {
            if (accuracy < bestAccuracy) {
                bestResult = location;//from w w  w  .ja v a2  s.  c o  m
                bestAccuracy = accuracy;
                bestTime = time;
            }
        }
    }

    return bestResult;
}

From source file:Main.java

/**
 * Tells whether location is enabled./*from  w  ww.ja v  a  2 s  .  c  o 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 Location getLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // Gets the last known location from the best service.
    String bestLocationProvider = locationManager.getBestProvider(new Criteria(), true /*enabled only*/);
    return locationManager.getLastKnownLocation(bestLocationProvider);
}

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 {/*from  w w  w . j  a  v a 2 s  . c o m*/
        return null;
    }
}

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 {//from   w w w  .  j a v a2  s  . c o 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

/**
 * To be used if you just want a one-shot best last location, iterates over
 * all providers and returns the most accurate result.
 */// w  w w.  ja  v  a2 s.com
public static Location getBestLastGeolocation(Context context) {
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = manager.getAllProviders();

    Location bestLocation = null;
    for (String it : providers) {
        Location location = manager.getLastKnownLocation(it);
        if (location != null) {
            if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) {
                bestLocation = location;
            }
        }
    }

    return bestLocation;
}