Java Code Examples for android.location.LocationManager#isProviderEnabled()

The following examples show how to use android.location.LocationManager#isProviderEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From PresencePublisher with MIT License 6 votes vote down vote up
private void checkLocationAccessAndBluetoothAndBatteryOptimizationAndStartWorker() {
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (needsLocationService
            && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED
            && (locationManager == null || !(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)))) {
        HyperLog.d(TAG, "Location service not yet enabled, asking user ...");
        FragmentManager fm = getSupportFragmentManager();

        ConfirmationDialogFragment fragment = ConfirmationDialogFragment.getInstance(ok -> {
            if (ok) {
                startActivityForResult(new Intent(ACTION_LOCATION_SOURCE_SETTINGS), LOCATION_REQUEST_CODE);
            }
        }, R.string.location_dialog_title, R.string.location_dialog_message);
        fragment.show(fm, null);
    } else {
        checkBluetoothAndBatteryOptimizationAndStartWorker();
    }
}
 
Example 2
Source File: LocationUtil.java    From mirror with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ResourceType")
public static boolean isLocationAvailable(final Context context) {
    final LocationManager manager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Timber.d("NETWORK_PROVIDER is enabled");
        manager.removeUpdates(LOCATION_LISTENER);
        if (manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) == null) {
            manager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    LOCATION_LISTENER
            );
        } else {
            return true;
        }
    } else {
        Timber.e("NETWORK_PROVIDER is disabled");
    }
    return false;
}
 
Example 3
Source File: NearbyTimeLineActivity.java    From iBeebo with GNU General Public License v3.0 6 votes vote down vote up
private void addLocation() {
    LocationManager locationManager = (LocationManager) NearbyTimeLineActivity.this
            .getSystemService(Context.LOCATION_SERVICE);

    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Toast.makeText(NearbyTimeLineActivity.this, getString(R.string.please_open_gps), Toast.LENGTH_SHORT).show();
        return;
    }

    Toast.makeText(NearbyTimeLineActivity.this, getString(R.string.gps_is_searching), Toast.LENGTH_SHORT).show();

    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
    }
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
    }
}
 
Example 4
Source File: LocationCoarseTest.java    From AndPermission with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    LocationManager locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    boolean networkProvider = providers.contains(LocationManager.NETWORK_PROVIDER);
    if (networkProvider) {
        return true;
    }

    PackageManager packageManager = mContext.getPackageManager();
    boolean networkHardware = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK);
    if (!networkHardware) return true;

    return !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
 
Example 5
Source File: Util.java    From androidnative.pri with Apache License 2.0 5 votes vote down vote up
static void getGPSStatus() {
      Activity activity = org.qtproject.qt5.android.QtNative.activity();
      LocationManager manager = (LocationManager) activity.getSystemService( activity.LOCATION_SERVICE );
      boolean gpsstatus = manager.isProviderEnabled( LocationManager.GPS_PROVIDER );
      Map reply = new HashMap();
      reply.put("gpsstatus", gpsstatus );
      SystemDispatcher.dispatch(GOT_GPS_STATUS_MSG,reply);
}
 
Example 6
Source File: MeasurementService.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void initPassive() {
    if (passiveLocationListener == null) {
        passiveLocationListener = new CommonLocationListener(this, LISTENER.PASSIVE);
    }
    passiveLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
            PackageManager.PERMISSION_GRANTED
            && passiveLocationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
        passiveLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
                minTimeDelay, 0, passiveLocationListener);
    }
}
 
Example 7
Source File: LocationCoarseTester.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    LocationManager locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    boolean networkProvider = providers.contains(LocationManager.NETWORK_PROVIDER);
    if (networkProvider) {
        return true;
    }

    PackageManager packageManager = mContext.getPackageManager();
    boolean networkHardware = packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_NETWORK);
    if (!networkHardware) return true;

    return !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
 
Example 8
Source File: Util.java    From snowplow-android-tracker with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the location of the android
 * device.
 *
 * @param context the android context
 * @return the phones Location
 */
public static Location getLastKnownLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location location = null;

    try {
        String locationProvider = null;
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            List<String> locationProviders = locationManager.getProviders(true);
            if (locationProviders.size() > 0) {
                locationProvider = locationProviders.get(0);
            }
        }

        if (locationProvider != null && !locationProvider.equals("")) {
            location = locationManager.getLastKnownLocation(locationProvider);
        }
    } catch (SecurityException ex) {
        Logger.e(TAG, "Exception occurred when retrieving location: %s", ex.toString());
    }

    return location;
}
 
Example 9
Source File: RNLocationModule.java    From react-native-gps with MIT License 5 votes vote down vote up
@Nullable
private static String getValidProvider(LocationManager locationManager, boolean highAccuracy) {
  String provider =
    highAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER;
  if (!locationManager.isProviderEnabled(provider)) {
    provider = provider.equals(LocationManager.GPS_PROVIDER)
      ? LocationManager.NETWORK_PROVIDER
      : LocationManager.GPS_PROVIDER;
    if (!locationManager.isProviderEnabled(provider)) {
      return null;
    }
  }
  return provider;
}
 
Example 10
Source File: Helpers.java    From test-samples with Apache License 2.0 5 votes vote down vote up
public static boolean isGpsOn(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (gpsEnabled) {
        return true;
    } else {
        return false;
    }
}
 
Example 11
Source File: PermissionManager.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public static final boolean isLocationEnable(Context context) {
    if (VERSION.SDK_INT < 23) {
        return true;
    }
    LocationManager locationManager = (LocationManager) context.getSystemService("location");
    boolean networkProvider = locationManager.isProviderEnabled(BlockInfo.KEY_NETWORK);
    boolean gpsProvider = locationManager.isProviderEnabled("gps");
    if (networkProvider || gpsProvider) {
        return true;
    }
    return false;
}
 
Example 12
Source File: GetFixHelper.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isPassiveProviderEnabled(Context context)
{
    LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    return (locationManager != null && locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER));
}
 
Example 13
Source File: GPSTesterActivityController.java    From android-gps-test-tool with Apache License 2.0 4 votes vote down vote up
private static void writeResultToTable(
			Location location,
			LocationManager locationManager,
			String elapsedTimeGPSProvider,
			Boolean isNetworkAvailable){
		
    	_gpsLatitude = location.getLatitude();
    	_gpsLongitude = location.getLongitude();
    	_gpsAccuracy = location.getAccuracy();  
    	final double speed = location.getSpeed();
    	final double altitude = location.getAltitude();

    	String bestAvailableText = "";
    	
		boolean networkProviderEnabled = false; 	
		boolean gpsProviderEnabled = false;
		
		try{
			networkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			gpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		}
		catch(Exception exc){
			Log.d("GPSTester","WriteResultToTable(): " + exc.getMessage());
		}

    	if(_networkAccuracy > _gpsAccuracy && _gpsAccuracy > 0.0 && gpsProviderEnabled == true){
    		_bestAvailableType = BestAvailableType.GPS;
    		bestAvailableText = "<b><font color='yellow'>Best Accuracy</font> = <font color='red'>GPS</b></font>" +
    				"<br><b>GPS Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" +
    				"<br><b>GPS Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude;
    	}  	
    	else if(_gpsAccuracy > _networkAccuracy && _networkAccuracy > 0.0 && networkProviderEnabled == true){
    		_bestAvailableType = BestAvailableType.NETWORK;	    		
    		bestAvailableText = "<b><font color='yellow'><b><font color='yellow'>Best Accuracy</font> = <font color='red'>Network</b></font></b></font>" +
    				"<br><b>Network Accuracy:<b/> " + DECIMAL_FORMAT_4.format(_networkAccuracy) + " meters" +
    				"<br><b>Network Lat/Lon:<b/> " + _networkLatitude + ", " + _networkLongitude;
    	}
    	else if(_gpsAccuracy == 0.0 && _networkAccuracy > 0.0 && networkProviderEnabled == true){
    		_bestAvailableType = BestAvailableType.NETWORK;	    		
    		bestAvailableText = "<b><font color='yellow'><b><font color='yellow'>Best Accuracy</font> = <font color='red'>Network</b></font></b></font>" +
    				"<br><b>Network Accuracy:<b/> " + DECIMAL_FORMAT_4.format(_networkAccuracy) + " meters" +
    				"<br><b>Network Lat/Lon:<b/> " + _networkLatitude + ", " + _networkLongitude;    		
    	}
    	else if(_networkAccuracy == 0.0 && _gpsAccuracy > 0.0 && gpsProviderEnabled == true){
    		_bestAvailableType = BestAvailableType.GPS;
    		bestAvailableText = "<b><font color='yellow'>Best Accuracy</font> = <font color='red'>GPS</b></font>" +
    				"<br><b>GPS Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" +
    				"<br><b>GPS Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude;    		
    	}
    	else{
    		_bestAvailableType = BestAvailableType.NULL;
    		bestAvailableText = "<b><font color='yellow'>Best Accuracy = N/A</b></font>" + 
    				"<br><b>Lat/Lon:</b> N/A" +  
    				"<br><b>Accuracy:</b> N/A";	    		
    	}
    	
	  	setBestAvailableImageView(_bestAvailableType);
		
		String elapsedTimeSinceLastGPS = _elapsedTimer.calculateTimeDifference(_initialGPSTime, _elapsedTimer.getElapsedtime());			  	
    	_initialGPSTime = _elapsedTimer.getElapsedtime();		
    	
	  	final String gpsLocationText = "<b><font color='yellow'>GPS Provider</b></font>" +
    		  	"<br><b>Timestamp:</b> " + _elapsedTimer.convertMillisToMDYHMSS(location.getTime()) +
				"<br><b>1st update elapsed time:</b> " + elapsedTimeGPSProvider +
			  	"<br><b>Since last update:</b> " + elapsedTimeSinceLastGPS +
		  		"<br><b>Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude +
		  		"<br><b>DMSS:</b> " + 
		  			Location.convert(_gpsLatitude, Location.FORMAT_SECONDS) + ", " +					  			
		  			Location.convert(_gpsLongitude, Location.FORMAT_SECONDS) +
		  		"<br><b>Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" +
		  		"<br><b>Speed:</b> " + DECIMAL_FORMAT.format((speed * 2.2369)) + " mph" + ", " +
		  		DECIMAL_FORMAT.format(((speed * 3600)/1000)) + " km/h" +
		  		"<br><b>Altitude:</b> " + DECIMAL_FORMAT.format(altitude) + " m, " +
		  		DECIMAL_FORMAT.format(altitude * 3.2808) + " ft" +
		  		"<br><b>Bearing:</b> " + location.getBearing() + " deg";				  	
			  	
    	
	  	_gpsLocationTextView.setText(Html.fromHtml(gpsLocationText));	  	
    	_bestAvailableInfoTextView.setText(Html.fromHtml(bestAvailableText));
    	    	
    	//If true then we can draw on the map. Offline maps not available in this version
    	if(isNetworkAvailable == true){
			final int redMapGraphicSize = Integer.valueOf(_preferences.getString("pref_key_gpsGraphicSize", "10"));    	
	    	
	    	// Called when a new location is found by the network location provider.
	    	if(_preferences.getBoolean("pref_key_centerOnGPSCoords", true) == true){
	    		_map.centerAt(_gpsLatitude, _gpsLongitude, true);
	    	}
	    	if(_preferences.getBoolean("pref_key_accumulateMapPoints", true) == false){
//	    		_map.clearPointsGraphicLayer();
	    		_graphicsLayer.removeAll();
	    	}
	    	
	    	addGraphicLatLon(_gpsLatitude, _gpsLongitude, null, SimpleMarkerSymbol.STYLE.CIRCLE,Color.RED,redMapGraphicSize,_graphicsLayer,_map);
    	}
	}
 
Example 14
Source File: DeviceUtils.java    From AndroidApp with Mozilla Public License 2.0 4 votes vote down vote up
public static boolean isGPSEnabled(Context mContext) {
    LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 15
Source File: MapActivity.java    From nongbeer-mvp-android-demo with Apache License 2.0 4 votes vote down vote up
private boolean isLocationEnable(){
    LocationManager locationManager =
            (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
    return locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER );
}
 
Example 16
Source File: MapsAppActivity.java    From maps-app-android with Apache License 2.0 4 votes vote down vote up
private boolean locationTrackingEnabled() {
	LocationManager locationManager = (LocationManager) getApplicationContext()
			.getSystemService(Context.LOCATION_SERVICE);
	return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 17
Source File: DeviceInfo.java    From Cangol-appcore with Apache License 2.0 2 votes vote down vote up
/**
 * 判断GPS定位是否开启
 *
 * @param context
 * @return
 */
public static boolean isGPSLocation(Context context) {
    final LocationManager locationManager = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 18
Source File: NetworkStateUtil.java    From star-zone-android with Apache License 2.0 2 votes vote down vote up
/**
 * 判断GPS是否打开
 *ACCESS_FINE_LOCATION权限
 * @param context
 * @return
 */
public static boolean isGPSEnabled(Context context) {
    //获取手机所有连接LOCATION_SERVICE对象
    LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 19
Source File: NetUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 2 votes vote down vote up
/**
 * GPS是否打开
 *
 * @param context 上下文
 * @return Gps是否可用
 */
public static boolean isGpsEnabled(Context context) {
    LocationManager lm = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 20
Source File: RxLocationTool.java    From RxTools-master with Apache License 2.0 2 votes vote down vote up
/**
 * 判断Gps是否可用
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isGpsEnabled(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}