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

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


You need to use the latest version of Google Play service. latest version has one dialog to activate all required things to get the GPS.

From Android Developer Play Service documentation,

Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the location your app receives still depends greatly on the settings enabled on the device (GPS, wifi, airplane mode, and others). Using the new SettingsApi class, you can bring up a Location Settings dialog which displays a one-touch control for users to change their settings without leaving your app.

Link directs to Play Service version documentation. Version 7.0 has introduced this new prompt.


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'}


Ola Cabs is using the newly released Settings API to achieve this functionality. As per the new API the user is not required to navigate to the settings page to enable location services giving a seamless integration for the same. Please read this for more details.