Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to seamlessly enable location/gps without redirected to the location service window

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 774
    Comment on it

    Most of the apps requires the user to manually enable the gps setting which causes confusion with the user as they are redirected to the location services window outside the app. The below code will help you to seamlessly enable location/gps without redirected to the location service window.

    Step1: Needed dependencies in build.gradle file

    compile 'com.google.android.gms:play-services:9.4.0'

    Step2: Initializing GoogleApiClient and a static variable

    public static final int REQUEST_CHECK_SETTINGS = 101;
    private GoogleApiClient mGoogleApiClient;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
            mGoogleApiClient.connect();
    Button btnEnableLocation = (Button)findViewById(R.id.btnEnableLocation);

    Step3: Accessing location dialog and to allow or cancel gps.

    btnEnableLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    locationDialog();
                }
            });
    private void locationDialog(){
    
            LocationRequest mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(30 * 1000);
            mLocationRequest.setFastestInterval(5 * 1000);
    
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
            builder.setAlwaysShow(true);
    
            PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    
            result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
                @Override
                public void onResult(LocationSettingsResult result) {
                    final Status status = result.getStatus();
                    //final LocationSettingsStates state = result.getLocationSettingsStates();
                    switch (status.getStatusCode()) {
                        case LocationSettingsStatusCodes.SUCCESS:
                            // All location settings are satisfied. The client can initialize location
                            // requests here.
                            //...
                            break;
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the user
                            // a dialog.
                            try {
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                            } catch (IntentSender.SendIntentException e) {
                                // Ignore the 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;
                    }
                }
            });
        }

    Step4: Used to check Location enabled or not

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            //final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
            switch (requestCode) {
                case REQUEST_CHECK_SETTINGS:
                    switch (resultCode) {
                        case Activity.RESULT_OK:
                            // All required changes were successfully made
                            if (mGoogleApiClient.isConnected()) {
    
                                Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
                            }
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                            // The user was asked to change settings, but chose not to
                            break;
                        default:
                            break;
                    }
                    break;
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: