可能的重複,編程開啓GPS在android系統
How to enable/disable gps and mobile data in android programmatically?
我已經看到的人,告訴他們可以在android系統編程打開GPS。但是我使用相同的代碼,並且無法做到這一點。
它只是顯示「正在搜索GPS ..」,但實際上並沒有這樣做。
這裏是我使用的代碼,
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
我也試過這個,
String provider = Settings.Secure.getString(context.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"));
context.sendBroadcast(poke);
}
但是,當我這樣做,
LocationManager manager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
boolean isGPSEnabled = manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
它返回我isGPSEnabled總是假的。如果我手動打開GPS,它將返回爲true。
任何機構都可以告訴我是否可以打開GPS?如果是這樣,我會出錯的地方。
問:在哪裏我要去錯了嗎?答:知道這個問題是重複的,但仍然會繼續發佈。 – NickT