0

我需要以编程方式启用 GPS,以便我使用以下代码启用 GPS:

            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);

但是当我使用这段代码时,我得到了以下异常:

    01-25 12:11:37.218: W/ActivityManager(477): Permission denied: checkComponentPermission() reqUid=1000
    01-25 12:11:37.218: W/ActivityManager(477): Permission Denial: broadcasting Intent { cat=[android.intent.category.ALTERNATIVE] dat=3 cmp=com.android.settings/.widget.SettingsAppWidgetProvider } from com.gpstraking (pid=982, uid=10186) requires null due to receiver com.android.settings/com.android.settings.widget.SettingsAppWidgetProvider

我必须给予什么许可才能避免此异常并启用 GPS。

4

2 回答 2

1

您的清单中是否有以下行:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
于 2013-01-25T07:05:10.433 回答
0

将此添加到清单中:

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>
于 2013-01-25T07:08:31.717 回答