【问题标题】:enable GPS programmatically problem after gms:play-services-location upgrading to version 21.0.0gms:play-services-location 升级到版本 21.0.0 后以编程方式启用 GPS 问题
【发布时间】:2022-11-10 00:24:47
【问题描述】:

在上次将 com.google.android.gms:play-services-location 更新到版本 21.0.0 之前,我可以使用下一个代码来激活 GPS 设备:

LocationRequest locationRequest = LocationRequest.create();
                        locationRequest.setPriority(Priority.PRIORITY_HIGH_ACCURACY);
                        locationRequest.setInterval(10000);
                        locationRequest.setFastestInterval(Priority.PRIORITY_HIGH_ACCURACY);
                        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                                .addLocationRequest(locationRequest);
                        SettingsClient client = LocationServices.getSettingsClient(context);
                        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

                        task.addOnFailureListener((Activity) context, e -> {
                            if (e instanceof ResolvableApiException) {
                                try {
                                    ResolvableApiException resolvable = (ResolvableApiException) e;
                                    resolvable.startResolutionForResult((Activity) context,
                                            23);
                                } catch (IntentSender.SendIntentException sendEx) {
                                    sendEx.printStackTrace();
                                }
                            }
                        });

更新到版本 21 后,我的代码如下所示:

 LocationRequest locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
                                .setWaitForAccurateLocation(false)
                                .setIntervalMillis(10000)
                                .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
                                .build();
                        
                      
                        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                                .addLocationRequest(locationRequest);
                        SettingsClient client = LocationServices.getSettingsClient(context);
                        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

                        task.addOnFailureListener((Activity) context, e -> {
                            if (e instanceof ResolvableApiException) {
                                try {
                                    ResolvableApiException resolvable = (ResolvableApiException) e;
                                    resolvable.startResolutionForResult((Activity) context,
                                            23);
                                } catch (IntentSender.SendIntentException sendEx) {
                                    sendEx.printStackTrace();
                                }
                            }
                        });

你怎么能看到 locationRequest 遭受了用 builder() 替换 create() 的修饰符。

问题是 GPS 激活菜单不再显示。正在寻找新异常不是 ResolvableApiException。

知道如何再次使用启用 GPS 消息吗?

我不想使用seetings方式:

((ActivityHome)context).startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

【问题讨论】:

标签: java android-studio exception gps


【解决方案1】:

最新版本有这个问题,选择低版本。

dependencies
{
 implementation 'com.google.android.gms:play-services-location:21.0.0'
}

改成...

dependencies
{
    implementation gms:play-services-location upgrading to version 20.0.0
}

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2012-11-22
    • 1970-01-01
    • 2021-03-12
    • 2019-05-26
    相关资源
    最近更新 更多