检查android开发中的gps状态

时间:2018-04-27 04:34:45

标签: java android

我正在创建一个Android应用程序,我需要通过融合位置提供程序获取用户位置。在获取位置之前,我想检查gps是否打开。我想在不使用位置管理器的情况下执行此操作。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

抛出波纹管方法,您可以检查位置模式,

CKEditor

答案 1 :(得分:0)

   private void checkGpsEnabled() {

        LocationSettingsRequest settingsRequest = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest)
                .setNeedBle(true)
                .setAlwaysShow(true)
                .build();
        SettingsClient client = LocationServices.getSettingsClient(this);
        Task<LocationSettingsResponse> response = client.checkLocationSettings(settingsRequest);

        response.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {

            }
        });
        response.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                if (e instanceof ResolvableApiException) {
                    ResolvableApiException resolvableApiException = (ResolvableApiException) e;
                    try {

                        resolvableApiException.startResolutionForResult(LocationFusedActivity.this, 1002);
                    } catch (IntentSender.SendIntentException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
    }