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

Par*_*rth 21 android android-location google-play-services android-settings android-5.0-lollipop

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

当我们在Lollipop中使用Google地图会发生这种情况.即使位置已禁用,也会在用户从地图应用输入后以高精度模式打开,而无需访问"设置".

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

startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));

哪个可以在BluetoothAdapter上找到,现在我们知道没有LocationAdapter,所以我环顾了gms-> LocationServices,基本上几乎所有的位置API引用,android.location.LocationManager,但似乎没有任何像ACTION_REQUEST_ENABLE现在可用.

希望有相同的其他方法,更多人尝试过.

请注意:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));不起作用.

Par*_*rth 23

更新3:

提示用户将位置设置更改 为@patrickandroid,在评论中提到,第二次更新的链接已损坏

GoogleSamples; android位置和选项 - 用于代码参考.

更新2:

GoogleSamples; - 用于代码参考.

此示例基于此repo中包含的LocationUpdates示例构建,并允许用户使用位置对话框更新设备的位置设置.

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


更新1:

Google Play服务7.0版(2015年3月)发布...

位置设置 - 虽然FusedLocationProviderApi结合了多个传感器以提供最佳位置,但应用程序接收的位置的准确性仍然在很大程度上取决于设备上启用的设置(GPS,wifi,飞行模式等).使用新的SettingsApi类,您可以打开一个"位置设置"对话框,该对话框显示一键式控件,用户无需离开您的应用即可更改其设置.

使用公共界面SettingsApi

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


离开上一部分以供参考:
更新/答案
对于每个寻找此答案的人,Google Play Services 7.0
它会添加用于检测地点和连接到附近设备的API,改进移动广告,健身数据,位置设置等

在Google Play服务7.0中,我们引入了一种标准机制来检查是否已启用必要的位置设置以使给定的LocationRequest成功.如果有可能的改进,您可以显示单触控件,以便用户无需离开您的应用即可更改其设置.

正是这样

此API提供了一个很好的机会,可以提供更好的用户体验,特别是如果位置信息对您的应用的用户体验至关重要,例如Google地图集成"位置设置"对话框时的情况,并且处于良好位置状态的用户数量.

资料来源: Android开发者博客:Google Play服务7.0 - 地点所有人!

SDK即将推出!
我们将在未来几天内推出Google Play服务7.0.在推出完成后,预计会对此博客文章,已发布的文档以及SDK的可用性进行更新.

将在实施后更新程序化loc


Psy*_*Gik 21

根据上面的@ 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;
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

并使用它,像

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

    locationChecker(mGoogleApiClient, MyActivity.this);
Run Code Online (Sandbox Code Playgroud)

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