2011-09-09 44 views

回答

5

編輯:這個解決方案也將關閉WiFi,藍牙等等

如果你想只關掉收音機,我認爲這是與此相關的問題:http://code.google.com/p/android/issues/detail?id=1065 我真的很悲觀找到好的解決方案,但好奇看到其他答案。

查看博客文章Android: Controlling Airplane Mode

// Toggle airplane mode. 
Settings.System.putInt(
     context.getContentResolver(), 
     Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1); 

// Post an intent to reload. 
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
intent.putExtra("state", !isEnabled); 
sendBroadcast(intent); 

其中isEnabled是飛行模式是否啓用。

別忘了你需要的WRITE_SETTINGS允許這樣做,雖然。

+0

+1很好的解決方案 –

+0

當你將手機切換到飛行模式也將抑制的Wi-Fi和藍牙。我不認爲這是預期的。主要任務是僅抑制GSM連接... – barmaley

+0

yes.i已經嘗試過飛行模式。這將禁用藍牙也。但我想禁用只有通話和短信是可能的?????我禁用GPRS。有什麼方法可用嗎? – Ram

5
/* Toggle airplane mode for 1 of the 4 allowed types 
* type allowed values: cell, wifi, bluetooth, nfc 
*/ 
private void changeRadioComponentEnabled(Context context, String type, boolean radio_component_enabled, boolean reset){  
     // now toggle airplane mode from on to off, or vice versa 
     Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, radio_component_enabled ? 0 : 1); 

     // now change system behavior so that only one component is turned off 
     // this also affects the future behavior of the system button for toggling air-plane mode. 
     // to reset it in order to maintain the system behavior, set reset to true, otherwise we lazily make sure mobile voice is always on 
     Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, type); 

     // post an intent to reload so the menu button switches to the new state we set 
     Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
     intent.putExtra("state", radio_component_enabled ? false : true); 
     context.sendBroadcast(intent); 

     // revert to default system behavior or not 
     if (reset){ Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell,bluetooth,wifi,nfc"); } 
     // if reset to default is not chosen, always enable mobile cell at least 
     // but only if NOT changing the mobile connection... 
     else if (type.indexOf("cell") == 0) { Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell");} 
}//end method 

當然,這需要許可android.permission.WRITE_SETTINGS,以及藍牙android.permission.BLUETOOTH_ADMIN。對於NFC,您可能需要android.permission.NFC

編輯:因爲原來的大量修改,因爲我以不同的方式在自己的應用程序實際使用這個

+0

我的手機在Settings.System.AIRPLANE_MODE_RADIOS中也有wimax:「cell,bluetooth,wifi,nfc,wimax」。這是令人驚訝的,因爲這款手機不支持4G WiMax! – pmont

相關問題