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

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

当我们在棒棒糖中使用Google Maps会发生这种情况即使地点已被禁用,用户从地图应用程序input后仍可在高精确度模式下打开,无需访问设置。

可以实现类似的function来启用蓝牙,其中的行动是在我的应用程序启动; 用户需要做出select,但用户不会被redirect到设置,使用:

startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));

它可以在BluetoothAdapter上find,现在我们知道没有LocationAdapter,所以我查看了周围gms-> LocationServices ,基本上几乎所有位置API引用下的android.location.LocationManager,但似乎没有任何像ACTION_REQUEST_ENABLE一样可用。

希望还有其他一些方法,也有更多的人尝试过。

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

更新2

GoogleSamples; LocationSettings – 用于代码参考。

此示例基于本回购库中包含的LocationUpdates示例构build,并允许用户使用位置对话框更新设备的位置设置。

使用SettingsApi确保设备的系统设置已根据应用程序的位置需求进行了正确configuration。


更新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。 预计此博客post的更新,发布的文档以及完成部署后SDK的可用性。

将在实施后更新编程loc

基于@ 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); 

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

我有解决scheme,在Android 6.0.1 Marshmallow上进行testing,button事件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需要启用USBdebugging或生根。 在adb shell执行以下命令(从设备上的根shell或从USB连接的计算机(如果设备没有root)):

 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" />