【发布时间】:2013-12-19 08:31:05
【问题描述】:
Android 是否允许以编程方式打开和关闭 GPS?
我正在使用下面的代码 [functions turnGPSon() & turnGPSoff()] (from here)
我正在使用“GPSStatus()”函数来检测 GPS 是否开启。
两个问题:
turnGPSoff() 不工作。 turnGPSon() 能够打开 GPS('我认为',因为我可以在左上角的状态图标中看到 GPS 符号,但是控制中心 [galaxy s3] 中的 GPS 按钮没有突出显示,并且我得到了准确的定位)
GPSStatus() 总是返回 false - 我检查的方法是否正确?
我的目标只是以编程方式打开和关闭 GPS? (是不是Android不允许通过编程方式开启和关闭GPS?答案here中提到)
private boolean GPSStatus() {
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
return gpsStatus;
}
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);
String provider = Settings.Secure.getString(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.sendBroadcast(poke);
}
}
// automatic turn off the gps
public void turnGPSOff()
{
myLogger.displayMsgs("in turnGPSoff()", true, true);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
【问题讨论】: