【问题标题】:Enable Location/GPS settings without using Play Services API在不使用 Play Services API 的情况下启用位置/GPS 设置
【发布时间】:2015-07-13 08:48:06
【问题描述】:

您可能已经知道,Google Play 服务中有一个新的位置设置对话框 API。

如果你不知道,这里是使用 Dialog API 的 Maps 应用的截图。

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

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

【问题讨论】:

  • 你找到解决方案了吗..+1 回答你的 grt 问题
  • 我觉得和你一样,google只在他们的api里面添加是很不公平的
  • 这是可行的,因为 Google 应用程序具有 root 访问权限。另一方面,Google 希望您使用他们自己的 API,因为他们的 API 将位置信息发送到 Google 服务器,因此您的程序将帮助 Google 更准确、更频繁地跟踪一个人的位置。欢迎来到资本主义 :) 但很好的问题,我一直在寻找相同的答案。

标签: android gps location settings google-play-services


【解决方案1】:

你好,这对我有用。

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

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 2015-02-15
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多