Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

enable GPS inside of application

// this function diplay a popup to user for enable GPS
private void TurnOnGPS(){
LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);
        Task<LocationSettingsResponse> result =
                LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
        result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
            @Override
            public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
                try {
                    LocationSettingsResponse response = task.getResult(ApiException.class);
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                } catch (ApiException exception) {
                    switch (exception.getStatusCode()) {
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the
                            // user a dialog.
                            try {
                                // Cast to a resolvable exception.
                                ResolvableApiException resolvable = (ResolvableApiException) exception;
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                resolvable.startResolutionForResult(
                                        SplashScreenActivity.this,
                                        LocationRequest.PRIORITY_HIGH_ACCURACY);
                            } catch (IntentSender.SendIntentException e) {
                                // Ignore the error.
                            } catch (ClassCastException e) {
                                // Ignore, should be an impossible error.
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            // Location settings are not satisfied. However, we have no way to fix the
                            // settings so we won't show the dialog.
                            break;
                    }
                }
            }
        });
}

// overide on activity result to have user action
 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case LocationRequest.PRIORITY_HIGH_ACCURACY:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        // All required changes were successfully made
                        Log.i("TAG", "onActivityResult: GPS Enabled by user");
                        break;
                    case Activity.RESULT_CANCELED:
                        // The user was asked to change settings, but chose not to
                        Log.i("TAG", "onActivityResult: User rejected GPS request");
                        break;
                    default:
                        break;
                }
                break;
        }
    }


Comment

PREVIOUS NEXT
Code Example
Java :: close current file android studio shortct 
Java :: split email on dot java 
Java :: textfield invisible java 
Java :: exchangerate api 
Java :: add external JARs to java vscode 
Java :: java split not working on comma 
Java :: Get generic type of class at runtime 
Java :: what is difference between constant and final in java 
Java :: 2d matrix multiplication 
Java :: random numeros negativos java 
Java :: springBoot Register a Custom Auto-Configuration 
Java :: what is collection fromework 
Java :: java nom de la methode actuel 
Java :: where do you use overriding in framework 
Java :: os compatible java path separator 
Java :: date.settime java 
Java :: java filewriter not working 
Java :: localdatetimw java input 
Java :: Get and set method uml 
Java :: ["org.elasticsearch.bootstrap.startupexception: java.lang.illegalstateexception: failed to obtain node locks, 
Java :: java jackson optional 
Java :: difference between string vs stringbuffer 
Java :: Java program to find which department has highest placement program 
Java :: fog command minecraft 
Java :: advantages of iterator in java 
Java :: add element to queue java 
Java :: java console readline null pointer exception 
Java :: get time until start of next hour in java 
Java :: decision tree drools using spring boot 
Java :: how to not allow a user to enter a mark greater than 100 or below 0 in java 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =