原問題來自於CSDN問答頻道,更多解決方案見:http://ask.csdn.net/questions/2046
問題描述:
我的應用中要開啓GPS,一直到android2.3.6版本都運行正常。但是到了android4.0版本以後,就不行了。
不知道爲什麼?在高版本里應該怎麼設置呢?
解決方案:
試試下面的代碼:
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.ctx.sendBroadcast(intent);
String provider = Settings.Secure.getString(ctx.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.ctx.sendBroadcast(poke);
}
}