Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Settings.Secure - LOCATION_MODE deprecated - alternative for writing

According to this the flag Settings.Secure.LOCATION_MODE has been depreacted in API 28. Is there an alternative way to write this setting? (I'm not interested in reading this value, which can be done via LocationManager)

The following works on my android pie device:

Settings.Secure.putInt(contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
Settings.Secure.putInt(contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_HIGH_ACCURACY)

Is it possible to get this exact same result when targetting > API 28 devices?.

like image 775
prom85 Avatar asked Mar 13 '19 10:03

prom85


1 Answers

I use this method to check if location service enabled or not

   public static Boolean isLocationEnabled(Context context) {        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // This is new method provided in API 28
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            return lm.isLocationEnabled();
        } else {
            // This is Deprecated in API 28
            int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
                    Settings.Secure.LOCATION_MODE_OFF);
            return (mode != Settings.Secure.LOCATION_MODE_OFF);
        }
    }

Edit : I misinterpreted the question here's how i do it to change location settings

/**
 * The desired interval for location updates. Inexact. Updates may be more or less frequent.
 */
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 5000;
/**
 * The fastest rate for active location updates. Updates will never be more frequent
 * than this value.
 */
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
        UPDATE_INTERVAL_IN_MILLISECONDS / 2;
/**
 * Small displacement for location updates
 */
private static final int SHORTEST_DISTANCE_IN_METER = 5;

ActivityResultLauncher<IntentSenderRequest> intentSenderForResult = registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult()
        , result -> {
            if (result.getResultCode() == RESULT_OK) {

            } else {

            }
        });

private LocationRequest createLocationRequest(){
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setPriority(Priority.PRIORITY_HIGH_ACCURACY);
        locationRequest.setSmallestDisplacement(SHORTEST_DISTANCE_IN_METER);
        return locationRequest;

}

  

private void changeLocationSettings(){
      //Ask to turn on location setting if not enabled
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            SettingsClient client = LocationServices.getSettingsClient(requireContext());
            Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
            task.addOnSuccessListener(locationSettingsResponse -> {
                // All location settings are satisfied. The client can initialize
                // location requests here.

            }).addOnFailureListener(e -> {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(resolvable.getResolution()).build();
                    intentSenderForResult.launch(intentSenderRequest);
                }
            });
}
like image 83
pintu236 Avatar answered Nov 20 '22 14:11

pintu236