嘗試那些:
private void turnGPSOn()
{
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);
}
}
private void turnGPSOff()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps"))
{ //if gps is 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);
}
}
請注意,你需要設置適當的應用程序的權限。
感謝您的回答。我寫沒有removerUpdates()和應用程序去背景的turnGPSOff()方法。 GPS關閉。 removeUpdates()和turnGPSOff()有什麼區別? – user3209380
和另一個問題。我希望當第二個答案禁用GPS發送鏈接,我的GPS禁用,但是,當我再次去應用程序,在onResume(),我如何檢測GPS禁用後,我啓用此功能,如果GPS關閉,我打開GPS!我用這個代碼檢測gps的開啓和關閉:(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) – user3209380