【发布时间】:2019-11-23 16:12:12
【问题描述】:
Google Maps 等应用程序显示启用 GPS 的对话框。
是否可以制作显示此类对话框以禁用 GPS 的应用程序?
【问题讨论】:
标签: android gps android-gps
Google Maps 等应用程序显示启用 GPS 的对话框。
是否可以制作显示此类对话框以禁用 GPS 的应用程序?
【问题讨论】:
标签: android gps android-gps
您不应该直接尝试启用/禁用 gps,而是official docs 建议您的应用指定所需的精度/功耗级别和所需的更新间隔,并且设备会自动对系统设置进行适当的更改.
我将在此处添加来自official docs 的代码以供立即参考,
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
// ...
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
【讨论】: