3

我想通过代码强制打开 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 中却不是。

4

3 回答 3

2

请使用此代码通过设置手动启用,因为由于 ics 中的安全原因,此功能已被删除。

Intent callGPSSettingIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(callGPSSettingIntent);
于 2013-10-23T05:57:03.057 回答
1

要启用 GPS,由于安全原因,Android 没有提供 API。因此,如果没有用户干预,您将无法打开 GPS。

要启用 Wi-Fi,

WifiManager wifiManager = new WifiManager()

if(wifiManager.isWifiEnabled())
     wifiManager.setWifiEnabled(false);

选择您想要的任何内容,将其设置为 ON 或 OFF。

于 2013-10-23T06:04:32.787 回答
0

您在问题中编写的代码以前运行良好,但由于它是安全漏洞,Android 开发人员现在已经修复了几天。

因此,即使您使用上面的代码,它也会给出 GPS 闪烁动画,但实际上它根本不起作用。即使从设备中删除应用程序后,您也可以看到 GPS 闪烁。

于 2013-10-23T05:52:39.253 回答