代码之家  ›  专栏  ›  技术社区  ›  Pararth

以编程方式启用位置模式高精度或节电,无需用户访问设置

  •  21
  • Pararth  · 技术社区  · 10 年前

    为什么我这样问: (也是在应用程序中尝试的原因)

    它发生了 当我们使用 棒棒糖中的谷歌地图 . 即使禁用了“位置”,也会在用户从“地图”应用程序输入后以高精度模式打开,而无需访问“设置”。

    启用蓝牙也可以实现类似的功能,即在我的应用程序中启动操作;用户需要做出选择,但不会使用以下方法将用户重定向到“设置”:

    startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));

    可以在上找到 BluetoothAdapter ,现在我们知道没有LocationAdapter,所以我四处寻找 gms->LocationServices 基本上 Location API references , android.location.LocationManager 但看起来不像 ACTION_REQUEST_ENABLE 目前可用。

    希望还有其他的方法,更多的人已经尝试过了。

    请注意:
    context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 不象那样工作。

    3 回复  |  直到 8 年前
        1
  •  23
  •   Pararth    6 年前

    更新3 :

    Prompt the user to change location settings 正如评论中提到的@patrickandroid,第二次更新的链接已断开

    GoogleSamples; android Location and options - for code reference.

    更新2 :

    GoogleSamples; - for code reference.

    此示例基于 repo,并允许用户更新设备的位置设置 使用位置对话框。

    使用SettingsPi确保设备的系统设置为 为应用程序的位置需求正确配置。


    更新1 :

    Google Play services, Version 7.0 (March 2015) released...

    位置设置 -尽管FusedLocationProviderApi结合了多个传感器为您提供最佳位置 应用程序接收的位置仍然很大程度上取决于设置 在设备上启用(GPS、wifi、飞机模式等)。使用 新的SettingsApi类,您可以打开“位置设置”对话框 它显示一个一键式控件,供用户更改其设置 而无需离开应用程序。

    使用 public interface SettingsApi

    • 确定设备上是否启用了相关系统设置以执行所需的位置请求。
    • (可选)调用一个对话框,该对话框允许用户通过一次点击来启用必要的位置设置。


    保留上一部分以供参考:
    更新/回答
    对于每个寻找这个答案的人来说, Google Play服务7.0
    它增加了用于检测地点和连接附近设备的API,改进了移动广告、健身数据、位置设置等

    在Google Play服务7.0中 检查是否为给定的 LocationRequest成功。如果有可能的改进,您 可以显示一个单触式控件,供用户更改其设置 而无需离开应用程序。

    exactly this

    这个API提供了一个很好的机会,可以让用户变得更好 经验,尤其是如果位置信息对 您的应用程序的用户体验,例如在 他们整合了“位置设置”对话框 处于良好位置状态的用户数量的增加。

    资料来源: Android developers blog: Google Play services 7.0 - Places Everyone!

    SDK即将推出!
    我们将推出Google Play服务7.0 接下来的几天。期待此博客文章的更新,已发布 文档和SDK的可用性 完整的。

    将在实施后更新编程的l-o-c

        2
  •  22
  •   PsyGik    10 年前

    根据上面的@user2450263注释,以下是一些片段,

    /**
     * Prompt user to enable GPS and Location Services
     * @param mGoogleApiClient
     * @param activity
     */
    public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(
                                    activity, 1000);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the 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;
                }
            }
        });
    }
    

    使用它就像,

    mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 34992, this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    
        locationChecker(mGoogleApiClient, MyActivity.this);
    

    基本上,您的用户会得到这样的提示,他们可以在高精度模式下启用位置,而无需进入设置 Turn on GPS

        3
  •  1
  •   davidgro Henrique José dos Santos Neto    8 年前

    我有解决方案,在Android 6.0.1棉花糖上测试,按钮事件 OnClick 调用void:

    private void openGPSSettings() {
        //Get GPS now state (open or closed)
        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
    
        if (gpsEnabled) {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
        } else {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
        }
    }
    

    Android需要启用USB调试或将其作为根。在中执行以下命令 adb shell (从设备上的根外壳,或从通过USB连接的计算机(如果设备不是根外壳):

    pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS
    

    在应用清单中添加这些权限:

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />