[英]Disable GPS on Android Programmatically
I am trying to create an app for my own use that enables/disables certain features like WiFi, GPS, etc; 我正在尝试创建自己的应用程序,以启用/禁用某些功能,如WiFi,GPS等; my question pertains to GPS. 我的问题与GPS有关。 Note that my phone is not rooted and I would like to keep it that way. 请注意,我的手机没有植根,我想保持这种状态。
I have successfully enabled GPS using the following code: 我已经使用以下代码成功启用了GPS:
public void toggleGPS() {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
LocationRequest locationRequest = new LocationRequest();
if (isGPSEnabled()) {
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(60 * 60 * 1000);
locationRequest.setFastestInterval(60 * 60 * 1000);
} else {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
}
googleApiClient.connect();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
Status status = result.getStatus();
LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
}
}
});
}
So when GPS isn't on, this code prompts me to turn it on, as I want. 因此,当GPS未打开时,此代码会提示我根据需要将其打开。 However, it doesn't seem to shut off when I request Balanced Power and set the larger intervals and no other app is using GPS. 但是,当我请求“平衡功率”并设置较大的间隔并且没有其他应用程序正在使用GPS时,它似乎并没有关闭。 I assumed that if my app no longer needs high accuracy, then GPS will automatically be switched off. 我以为如果我的应用不再需要高精度,那么GPS将自动关闭。 Am I misunderstanding something? 我误会了吗? And is there any way to switch it off? 有没有办法将其关闭? I can sort of understand the security concerns for disallowing GPS to be ENABLED programmatically, but I don't understand why Android wouldn't allow it to be DISABLED. 我可以某种程度上理解不允许以编程方式启用GPS的安全问题,但我不明白为什么Android不允许禁用GPS。
Thanks in advance! 提前致谢!
try this code it works on all versions, make a try .... 试试这个代码,它适用于所有版本,请尝试....
// automatic turn off the gps
public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.ctx.sendBroadcast(poke);
}
}
and code to enable is as follow you can use this.. 启用的代码如下,您可以使用它。
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.ctx.sendBroadcast(intent);
String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.ctx.sendBroadcast(poke);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.