Turn GPS on programmatically like Ola Cabs Android app

前端 未结 7 891
忘了有多久
忘了有多久 2021-02-04 15:44

I am working on an application and need to integrate GPS location. I want to switch the GPS on programmatically. The condition is: I don\'t want to send the user to the Setting

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 16:40

    Check the code below. Firstly, it will check if location is enabled or not and will prompt user to enable the location.

    private GoogleApiClient googleApiClient;
    
    final static int REQUEST_LOCATION = 199;
    
    
    // check whether gps is enabled
    public boolean noLocation() {
            final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                //  buildAlertMessageNoGps();
    
                enableLoc();
                return true;
            }
            return false;
    
        }
    
    
     private void enableLoc() {
    
    if (googleApiClient == null) {
                googleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                            @Override
                            public void onConnectionFailed(ConnectionResult connectionResult) {
    
                                Timber.v("Location error " + connectionResult.getErrorCode());
                            }
                        }).build();
                googleApiClient.connect();
    
                LocationRequest locationRequest = LocationRequest.create();
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                locationRequest.setInterval(30 * 1000);
                locationRequest.setFastestInterval(5 * 1000);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                        .addLocationRequest(locationRequest);
    
                builder.setAlwaysShow(true);
    
                PendingResult result =
                        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
                result.setResultCallback(new ResultCallback() {
                    @Override
                    public void onResult(LocationSettingsResult result) {
                        final Status status = result.getStatus();
                        switch (status.getStatusCode()) {
                            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                try {
                                    // Show the dialog by calling startResolutionForResult(),
                                    // and check the result in onActivityResult().
                                    status.startResolutionForResult(
                                            (Activity) context, REQUEST_LOCATION);
                                } catch (IntentSender.SendIntentException e) {
                                    // Ignore the error.
                                }
                                break;
                        }
                    }
                });
            }
    
    
    
            @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            switch (requestCode) {
                case REQUEST_LOCATION:
                    switch (resultCode) {
                        case Activity.RESULT_CANCELED: {
                            // The user was asked to change settings, but chose not to
                            finish();
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                    break;
            }
    
        }
    
    }
    

提交回复
热议问题