首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FusedLocationProviderApi时为Location Services自定义警报消息

使用FusedLocationProviderApi时为Location Services自定义警报消息
EN

Stack Overflow用户
提问于 2019-10-22 14:30:18
回答 1查看 482关注 0票数 3

我正在使用Fused Location Provider API在我的项目中获取device location。当位置服务关闭时,它显示了一个alert dialog,它要求用户打开它。

是否有任何方式自定义此警报对话框以显示不同的消息?

代码:

代码语言:javascript
复制
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
                    }
                }
            }
EN

回答 1

Stack Overflow用户

发布于 2019-10-22 14:46:34

尝试下面的代码行

代码语言:javascript
复制
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);
    }
}

希望它能对你有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58506554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档