【问题标题】:Android - Location Using Play Service API - SettingsClient error in service classAndroid - 使用 Play 服务 API 的位置 - 服务类中的 SettingsClient 错误
【发布时间】:2018-09-12 04:19:06
【问题描述】:

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

需要用Activity来投射

如果我将它与 Activity 一起投射,则意味着编译时没有问题。

但在运行时它会给出类似的错误

请给出一些解决方法。

【问题讨论】:

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


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多