Android-使用Play Service API定位-服务类中的SettingsClient错误

时间:2018-09-12 04:19:06

标签: android location google-play-services android-service android-workmanager

我必须每30分钟获取一次用户当前位置。因此,我计划将Play Service API与Work Manager结合使用以在后台执行此操作(即使我关闭了应用程序也会获得位置)。如果我的位置设置为“开”,一切正常。我可以通过 SettingsClient 选项检查位置和权限是否可用。但是,SettingsClient仅在我在Activity上使用它时才有效。在Workmanager(Service)类中,当我尝试使用SettingsClient时,它显示如下图所示的错误

enter image description here

它需要使用Activity进行投射

enter image description here

如果我使用Activity强制转换,则表示编译时没有问题。 enter image description here

但是在运行时它给出了类似的错误 enter image description here

请提供一些解决问题的技巧。

1 个答案:

答案 0 :(得分:1)

我有解决此问题的方法,我认为可以发表。可能会帮助某人。

我已经更改了这样的编码

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequestHighAccuracy)
            .addLocationRequest(mLocationRequestBalancedPowerAccuracy);
        Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext()).checkLocationSettings(builder.build());

        result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
            @SuppressLint("MissingPermission")
            @Override
            public void onComplete(Task<LocationSettingsResponse> task) {
                try {
                    LocationSettingsResponse response = task.getResult(ApiException.class);
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

                } catch (ApiException exception) {
                    switch (exception.getStatusCode()) {
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the
                            // user a dialog.
                            try {
                                // Cast to a resolvable exception.
                                ResolvableApiException resolvable = (ResolvableApiException) exception;
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                             /*   resolvable.startResolutionForResult(
                                        OuterClass.this,
                                        REQUEST_CHECK_SETTINGS);*/
                                sendNotification("Failed", "Turn on Location" );
                            } catch (ClassCastException e) {
                                // Ignore, should be an impossible error.
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            // Location settings are not satisfied. However, we have no way to fix the
                            // settings so we won't show the dialog.
                            sendNotification("Failed", "Setting or not avaible" );

                            break;
                    }
                }
            }
        });

参考: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient