android - 不使用 Play Services API 启用位置/GPS 设置

标签 android gps location settings google-play-services

大多数人可能已经知道,Google Play 服务中有一个新的 Location Settings Dialog API。

对于那些不知道的人,这里是使用 Dialog API 的 Maps 应用程序的屏幕截图。

Google Maps Location Dialog

Google 仅在其播放服务中添加该 API 有点不公平。另一方面,我认为如果他们能做到,就会有解决方法。这就是我问这个问题的原因。任何信息表示赞赏。

(注意:有一个漏洞可以让开发人员以编程方式启用和禁用 gps/位置设置。但是,那些在 SO 上可用的技巧不再有效。请不要发布过时的答案。)

最佳答案

嗨,这对我有用。

void initApiClient() {
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(@Nullable Bundle bundle) {
                        showLocationSettingsDialog();
                    }

                    @Override
                    public void onConnectionSuspended(int i) {

                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult result) {
                        Log.i(TAG, "onConnectionFailed() connectionResult = [" + result + "]");
                    }
                })
                .build();
        mGoogleApiClient.connect();
    }

    void showLocationSettingsDialog() {

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);

        Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getActivity())
                .checkLocationSettings(builder.build());

        result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
            @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.
                } catch (ApiException e) {
                    switch (e.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) e;
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                resolvable.startResolutionForResult(
                                        getActivity(),
                                        REQUEST_CHECK_SETTINGS);
                            } catch (IntentSender.SendIntentException exception) {
                                // Ignore the error.
                            } catch (ClassCastException exception) {
                                // 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.
                            break;
                    }
                }
            }
        });
    }

more info

关于android - 不使用 Play Services API 启用位置/GPS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022133/

相关文章:

android - 向导卡住时如何将 "Add an App Engine Module"发送到 Android Studio

android - apk文件名更改

ios - 在 iOS 上使用 GPS 的正确方法

java - 在设备所有者应用程序中启用 GPS

autocomplete - 谷歌地方 API : Is the option to switch off location biasing been removed from auto complete API?

ios - 如何在不使用委托(delegate)的情况下获取当前位置?

android - 如何在 Flutter 中让 alertDialog 在几秒钟后自动消失?

android - SHA 1 哈希键对所有应用程序都相同吗?

android - 负高度值

android location getting null 需要解决办法