Use the Android Default GPS ON-OFF dialog in My application

后端 未结 4 1100
野的像风
野的像风 2021-02-07 04:58

In Our application we want to integrate the android Default GPS Dialog. The same dialog that appears when the GPS is OFF and we press of my location button in google map apps. H

4条回答
  •  别那么骄傲
    2021-02-07 05:17

    
    import android.content.Intent;
    import android.content.IntentSender;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    
    import com.google.android.gms.common.ConnectionResult;
    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.common.api.Status;
    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.LocationSettingsStatusCodes;
    
    public class LocSettingsActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback {
    
        protected GoogleApiClient mGoogleApiClient;
        protected LocationRequest locationRequest;
        int REQUEST_CHECK_SETTINGS = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_loc_settings);
    
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
            mGoogleApiClient.connect();
    
            locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(30 * 1000);
            locationRequest.setFastestInterval(5 * 1000);
        }
    
        @Override
        public void onConnected(@Nullable Bundle bundle) {
    
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            builder.setAlwaysShow(true);
            PendingResult result =
                    LocationServices.SettingsApi.checkLocationSettings(
                            mGoogleApiClient,
                            builder.build()
                    );
    
            result.setResultCallback(this);
    
        }
    
        @Override
        public void onConnectionSuspended(int i) {
    
        }
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
        }
    
        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
    
                    // NO need to show the dialog;
    
                    break;
    
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    //  Location settings are not satisfied. Show the user a dialog
    
                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
    
                        status.startResolutionForResult(LocSettingsActivity.this, REQUEST_CHECK_SETTINGS);
    
                    } catch (IntentSender.SendIntentException e) {
    
                        //failed to show
                    }
                    break;
    
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are unavailable so not possible to show any dialog now
                    break;
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CHECK_SETTINGS) {
    
                if (resultCode == RESULT_OK) {
    
                    Toast.makeText(getApplicationContext(), "GPS enabled", Toast.LENGTH_LONG).show();
                } else {
    
                    Toast.makeText(getApplicationContext(), "GPS is not enabled", Toast.LENGTH_LONG).show();
                }
    
            }
        }
    }
    
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.google.android.gms:play-services-location:8.4.0'
    }
    

提交回复
热议问题