使用FusedLocationProviderApi时位置服务的自定义警报消息

问题描述 投票:0回答:1

我正在使用Fused Location Provider API在我的项目中获得device location。关闭位置服务后,它会显示alert dialog,要求用户将其打开。

有什么方法可以自定义此警报对话框以显示其他消息?

dialog

代码:

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
                    }
                }
            }
android kotlin android-location fusedlocationproviderapi android-fusedlocation
1个回答
0
投票

尝试下面的代码行

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();
        }
    }

希望对您有帮助

© www.soinside.com 2019 - 2024. All rights reserved.