以下代碼在Android 2.x.x版本中運行良好。它成功打開設備中的gps。但問題是它不適用於android版本4.x以上。以編程方式打開/關閉在Android版本4.x及以上版本中的gps
我期待在編程4.x版本起開啓GPS。任何幫助或解決方案?
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Utility.print("provider string " + provider);
if (!provider.contains("gps")) { // if gps is disabled
Utility.print("gps is disabled now enabled");
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);
Utility.print("gps is disabled now enabled 1");
} else {
Utility.print("gps is already enabled");
}
我見過很多應用程序將用戶導向到GPS設置屏幕,而不是直接設置它。 – alberge 2012-04-05 09:49:40