我需要以編程方式啓用GPS讓我用下面的代碼啓用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。
哪個版本的設備,您正在使用? –
http://stackoverflow.com/a/5305835/1395259 –
我已經在Android 2.3和Android 4.0.1上測試過 –