我想通过代码强制打开GPS和WiFi,而无需进行设置,所以我对如何在ICS中实现这一点提出了建议。
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"));
sendBroadcast(poke);
}
这段代码在2.1中运行良好,但在ICS中却没有。
答案 0 :(得分:2)
请使用此代码进行手动启用设置,因为出于安全原因,此功能已被删除。
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
答案 1 :(得分:1)
要启用GPS,由于安全性,Android没有提供API。因此,如果没有用户干预,您将无法打开GPS。
启用Wi-Fi,
WifiManager wifiManager = new WifiManager()
if(wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);
选择您想要的任何一种,将其设置为ON或OFF。
答案 2 :(得分:0)
你在问题中写的代码以前工作正常,但由于它是安全漏洞,Android开发人员现在已经修复了一天。
所以即使你使用上面的代码,它也会给出GPS闪烁动画,但实际上它根本不起作用。即使从设备中删除应用程序,您也可以看到GPS闪烁。