List of usage examples for android.content Context LOCATION_SERVICE
String LOCATION_SERVICE
To view the source code for android.content Context LOCATION_SERVICE.
Click Source Link
From source file:Main.java
/** * This method returns the last,best known current location for user * @param context/*from w ww . ja v a 2 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 ww w .ja v a2 s .c om*/ 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); }
From source file:Main.java
public static String getGpsString(Context context) { if (context == null) { return null; }/*from w w w. j av a 2 s . c o m*/ 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; }
From source file:Main.java
/** * Returns current (last known) location of the system. If the context is missing permissions * (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION) or location tracking is disabled by user, this * will fail and return null./* w w w .jav a 2 s.c o m*/ * * @param context * Application context of caller * @return Current location or null if we could not retrieve current location */ public static Location getCurrentLocation(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); // Find most accurate last known location Location location = null; for (int i = providers.size() - 1; i >= 0; i--) { location = lm.getLastKnownLocation(providers.get(i)); if (location != null) break; } return location; }
From source file:Main.java
public static boolean isGPSEnable(Context context) { LocationManager locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); return locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER); }
From source file:Main.java
public static boolean isNatworkEnable(Context context) { LocationManager locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); return locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER); }
From source file:Main.java
public static boolean isOpenGPS(Context context) { LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (!alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { return false; }//from w ww .j a va 2 s . c o m return true; }
From source file:Main.java
private static Location getLastBestLocation(Context pContext) { LocationManager locationManager = (LocationManager) pContext.getSystemService(Context.LOCATION_SERVICE); Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }/*from w w w.j a va 2s .co m*/ long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if (0 < GPSLocationTime - NetLocationTime) { Log.e(TAG, "Located by GPS"); return locationGPS; } else { Log.e(TAG, "Located by network"); return locationNet; } }
From source file:Main.java
/** Instantiates the mLocationManager variable if needed. */ private static final void loadLocationManager() { if (mLocationManager == null) { mLocationManager = (LocationManager) (new Activity().getApplicationContext()) .getSystemService(Context.LOCATION_SERVICE); }//from www . j a v a 2 s. com }
From source file:com.prey.actions.location.LocationUtil.java
public static HttpDataService dataLocation(Context ctx) { HttpDataService data = null;/*from w ww .j a v a 2s . c o m*/ try { LocationManager mlocManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); boolean isGpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean isNetworkEnabled = mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); PreyLogger.d("gps status:" + isGpsEnabled); PreyLogger.d("net status:" + isNetworkEnabled); PreyLocation location = null; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) { if (isGpsEnabled || isNetworkEnabled) { String method = getMethod(isGpsEnabled, isNetworkEnabled); PreyLocation locationPlay = null; int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(ctx); if (ConnectionResult.SUCCESS == resultCode) { location = getPreyLocationPlayService(ctx, method); } } } else { PreyLogger.d("ask for permission location"); } if (location == null) location = getDataLocationWifi(ctx); if (location != null) { PreyLogger.d("locationData:" + location.getLat() + " " + location.getLng() + " " + location.getAccuracy()); data = convertData(location); } else { sendNotify(ctx, "Error"); } } catch (Exception e) { sendNotify(ctx, "Error"); } return data; }