Customise alert message for Location Services when using FusedLocationProviderApi

Viewed 392

I am using Fused Location Provider API to get device location in my project. When the location services are off it shows an alert dialog asking the user to turn it on.

Is there any way to customize this alert dialog to show a different message?

dialog

Code:

LocationServices.getSettingsClient(this)
            .checkLocationSettings(locationRequestBuilder.build())
            .addOnCompleteListener {
                try { 
                    it.getResult(ApiException::class.java)
                    // Location settings are On
                } catch (exception: ApiException) { // Location settings are Off
                    when (exception.statusCode) {
                        RESOLUTION_REQUIRED -> try { // Check result in onActivityResult
                            val resolvable = exception as ResolvableApiException
                            resolvable.startResolutionForResult(this, LOCATION_REQUEST_CODE)
                        } catch (ignored: IntentSender.SendIntentException) {
                        } catch (ignored: ClassCastException) {
                        } 
                        // Location settings are not available on device
                    }
                }
            }
1 Answers

Try the below line of code

public void checkGpsStatus() {
        try {
            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);

            PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClientHome,   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:
                            hideGpsEnablDialog();
                            break;

                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            displayPromptForEnablingGPSSecond(MainActivity.this);
                            break;

                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                            break;
                    }
                }
            });
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Dialog locationDialog;
    public boolean isDialogopen;
    public void displayPromptForEnablingGPSSecond(final Activity activity) {
        if (isDialogopen) {
            return;
        }
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        try {
            locationDialog = new Dialog(activity);
            locationDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            locationDialog.setContentView(R.layout.dialog_confirm_type1);
            locationDialog.setCancelable(false);
            locationDialog.setCanceledOnTouchOutside(false);
            int width = activity.getWindowManager().getDefaultDisplay()
                    .getWidth();
            width = width - 80;
            locationDialog.getWindow().setLayout(width, ActionBar.LayoutParams.WRAP_CONTENT);

            TextView tv_message = (TextView) locationDialog
                    .findViewById(R.id.tv_message);

            tv_message.setText(""+getResources().getString(R.string.gps_enable_message));

            final TextView tv_ok = (TextView) locationDialog
                    .findViewById(R.id.tv_ok);

            final TextView tv_cancel = (TextView) locationDialog
                    .findViewById(R.id.tv_cancel);

            tv_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isDialogopen = false;
                    activity.startActivityForResult(new Intent(action), 11);
                    locationDialog.dismiss();
                }
            });

            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isDialogopen = false;
                    hideGpsEnablDialog();
                }
            });
            locationDialog.show();
            isDialogopen = true;
        } catch (Exception e) {
            ExceptionHandler.printStackTrace(e);
        }
    }

    public void hideGpsEnablDialog() {
        try {
            if (locationDialog != null && locationDialog.isShowing()) {
                locationDialog.dismiss();
            }
            isDialogopen = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private void turnGPSOn(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(!provider.contains("gps")){ 
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        sendBroadcast(poke);
    }
}

private void turnGPSOff(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(provider.contains("gps")){
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        sendBroadcast(poke);
    }
}

Hope it will help for you

Related