Android是否允许GPS以编程方式打开和关闭?如何打开和关闭GPS?
我使用下面(from here)
我使用的函数 “GPSStatus()” 使用的代码[功能turnGPSon()& turnGPSoff()],以检测是否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);
}