Turn GPS on programmatically like Ola Cabs Android app

前端 未结 7 881
忘了有多久
忘了有多久 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:48
    package com.example.user.myGPS;
    
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.content.IntentSender;
        import com.google.android.gms.common.api.GoogleApiClient;
        import com.google.android.gms.common.api.PendingResult;
        import com.google.android.gms.common.api.ResultCallback;
        import com.google.android.gms.location.LocationRequest;
        import com.google.android.gms.location.LocationServices;
        import com.google.android.gms.location.LocationSettingsRequest;
        import com.google.android.gms.location.LocationSettingsResult;
        import com.google.android.gms.location.LocationSettingsStates;
        import com.google.android.gms.location.LocationSettingsStatusCodes;
    
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        private GoogleApiClient googleApiClient;
        final int REQUEST_CHECK_SETTINGS = 0x1;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //-------Code to turn on GPS------------------
            if (googleApiClient == null) {
    
                googleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                        .addApi(LocationServices.API).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);
                builder.setNeedBle(true);
    
    
                PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
                result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
                    @Override
                    public void onResult(LocationSettingsResult result) {
                        final com.google.android.gms.common.api.Status status = result.getStatus();
                        final LocationSettingsStates state = result.getLocationSettingsStates();
                        switch (status.getStatusCode()) {
                            case LocationSettingsStatusCodes.SUCCESS:
                                break;
                            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                try
                                {
                                    status.startResolutionForResult(MainActivity.this,REQUEST_CHECK_SETTINGS);
                                } catch (IntentSender.SendIntentException e) {}
                                break;
                            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                                break;
                        }
                    }
                });
    
            }
    
    
        }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题