使用对话框禁用 GPS (Android)
Disable GPS using dialog box (Android)
Google 地图等应用程序会显示用于启用 GPS 的对话框。
是否可以制作显示此类对话框以禁用 GPS 的应用程序?
你不应该直接尝试 enable/disable gps,相反,official docs 建议你的应用指定所需的 accuracy/power 消耗水平和所需的更新间隔,以及设备自动对系统设置进行适当的更改。
我将在此处添加 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.
}
}
}
});
Google 地图等应用程序会显示用于启用 GPS 的对话框。
是否可以制作显示此类对话框以禁用 GPS 的应用程序?
你不应该直接尝试 enable/disable gps,相反,official docs 建议你的应用指定所需的 accuracy/power 消耗水平和所需的更新间隔,以及设备自动对系统设置进行适当的更改。
我将在此处添加 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.
}
}
}
});