使用对话框禁用 GPS (Android)
Posted
技术标签:
【中文标题】使用对话框禁用 GPS (Android)【英文标题】:Disable GPS using dialog box (Android) 【发布时间】:2019-11-23 16:12:12 【问题描述】:Google Maps 等应用程序显示启用 GPS 的对话框。
是否可以制作显示此类对话框以禁用 GPS 的应用程序?
【问题讨论】:
【参考方案1】:您不应该直接尝试启用/禁用 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.
);
【讨论】:
以上是关于使用对话框禁用 GPS (Android)的主要内容,如果未能解决你的问题,请参考以下文章
启用 GPS 的 LocationSettingsRequest 对话框