以编程方式启用定位模式高精度或省电,无需用户访问设置
Enabling Location mode High Accuracy or Battery saving, programmatically, without user needing to visit Settings
为什么我问这个:(也是在应用程序中尝试它的原因)
当我们在 Lollipop 中使用 Google 地图时,就会发生这种情况。 即使位置被禁用,它也会在用户从地图应用程序输入后以高精度模式打开,而无需访问设置。
启用蓝牙可以实现类似的功能,在我的应用程序中启动该操作;用户需要做出选择,但用户不会被重定向到设置,使用:
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
可以在 BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager 上找到,但似乎还没有像 ACTION_REQUEST_ENABLE
这样的东西可用。
希望有其他的方法,让更多人尝试过。
请注意:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
不是这样的。
更新 3:
Prompt the user to change location settings
正如@patrickandroid,在评论中提到,第二次更新的 link 已损坏
GoogleSamples; android Location and options - for code reference.
更新 2:
GoogleSamples; - for code reference.
This sample builds on the LocationUpdates sample included in this
repo, and allows the user to update the device's location settings
using a location dialog.
Uses the SettingsApi to ensure that the device's system settings are
properly configured for the app's location needs.
更新 1:
Google Play services, Version 7.0 (March 2015) released...
Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
location your app receives still depends greatly on the settings
enabled on the device (GPS, wifi, airplane mode, and others). Using
the new SettingsApi class, you can bring up a Location Settings dialog
which displays a one-touch control for users to change their settings
without leaving your app.
使用 public interface SettingsApi
- Determine if the relevant system settings are enabled on the device to carry out the desired location request.
- Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.
留上篇供参考:
Update/Answer
对于寻找此答案的每个人,Google Play Services 7.0
它添加了用于检测位置和连接到附近设备的 API,改进了移动广告、健身数据、位置设置等
In Google Play services 7.0, we’re introducing a standard mechanism to
check that the necessary location settings are enabled for a given
LocationRequest to succeed. If there are possible improvements, you
can display a one touch control for the user to change their settings
without leaving your app.
This API provides a great opportunity to make for a much better user
experience, particularly if location information is critical to the
user experience of your app such as was the case with Google Maps when
they integrated the Location Settings dialog and saw a dramatic
increase in the number of users in a good location state.
来源: Android developers blog: Google Play services 7.0 - Places Everyone!
SDK Coming Soon!
We will be rolling out Google Play services 7.0 over
the next few days. Expect an update to this blog post, published
documentation, and the availability of the SDK once the rollout is
completed.
将在实施后更新程序化 l-o-c
根据@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);
基本上你的用户会得到这样的提示,他们可以在高精度模式下启用定位,而无需进入设置
我有解决方案,在 Android 6.0.1 Marshmallow 上测试过,在按钮事件 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 调试或被 root。在 adb shell
中执行以下命令(从设备上的 root shell 或如果设备未 root,则从通过 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" />
为什么我问这个:(也是在应用程序中尝试它的原因)
当我们在 Lollipop 中使用 Google 地图时,就会发生这种情况。 即使位置被禁用,它也会在用户从地图应用程序输入后以高精度模式打开,而无需访问设置。
启用蓝牙可以实现类似的功能,在我的应用程序中启动该操作;用户需要做出选择,但用户不会被重定向到设置,使用:
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
可以在 BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager 上找到,但似乎还没有像 ACTION_REQUEST_ENABLE
这样的东西可用。
希望有其他的方法,让更多人尝试过。
请注意:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
不是这样的。
更新 3:
Prompt the user to change location settings 正如@patrickandroid,在评论中提到,第二次更新的 link 已损坏
GoogleSamples; android Location and options - for code reference.
更新 2:
GoogleSamples; - for code reference.
This sample builds on the LocationUpdates sample included in this repo, and allows the user to update the device's location settings using a location dialog.
Uses the SettingsApi to ensure that the device's system settings are properly configured for the app's location needs.
更新 1:
Google Play services, Version 7.0 (March 2015) released...
Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the location your app receives still depends greatly on the settings enabled on the device (GPS, wifi, airplane mode, and others). Using the new SettingsApi class, you can bring up a Location Settings dialog which displays a one-touch control for users to change their settings without leaving your app.
使用 public interface SettingsApi
- Determine if the relevant system settings are enabled on the device to carry out the desired location request.
- Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.
留上篇供参考:
Update/Answer
对于寻找此答案的每个人,Google Play Services 7.0
它添加了用于检测位置和连接到附近设备的 API,改进了移动广告、健身数据、位置设置等
In Google Play services 7.0, we’re introducing a standard mechanism to check that the necessary location settings are enabled for a given LocationRequest to succeed. If there are possible improvements, you can display a one touch control for the user to change their settings without leaving your app.
This API provides a great opportunity to make for a much better user experience, particularly if location information is critical to the user experience of your app such as was the case with Google Maps when they integrated the Location Settings dialog and saw a dramatic increase in the number of users in a good location state.
来源: Android developers blog: Google Play services 7.0 - Places Everyone!
SDK Coming Soon!
We will be rolling out Google Play services 7.0 over the next few days. Expect an update to this blog post, published documentation, and the availability of the SDK once the rollout is completed.
将在实施后更新程序化 l-o-c
根据@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);
基本上你的用户会得到这样的提示,他们可以在高精度模式下启用定位,而无需进入设置
我有解决方案,在 Android 6.0.1 Marshmallow 上测试过,在按钮事件 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 调试或被 root。在 adb shell
中执行以下命令(从设备上的 root shell 或如果设备未 root,则从通过 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" />