我想啓用/禁用Android手機的GSM連接。我需要根據需要禁用/啓用通話和短信。我該怎麼做?如何以編程方式禁用Android中的GSM連接
8
A
回答
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允許這樣做,雖然。
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
相關問題
- 1. 如何禁用GSM連接
- 2. 如何在android中以編程方式禁用移動數據連接?
- 3. Android - 以編程方式連接到wifi
- 4. 如何以編程方式禁用android中的「設置」選項?
- 5. Android以編程方式禁用onScroll()
- 6. 如何以編程方式禁用cookie
- 7. 在Windows 7上以編程方式啓用/禁用連接
- 8. 如何在android中以編程方式禁用拼寫校正
- 9. Android如何以編程方式啓用/禁用自動同步
- 10. 如何在rooted android上以編程方式禁用/啓用gps?
- 11. 如何以編程方式禁用佈局和其內容android
- 12. 如何以編程方式禁用android硬件按鈕?
- 13. 如何以編程方式啓用遠程桌面連接?
- 14. 如果在Android Wear應用程序中斷開連接,如何以編程方式「連接」安卓手錶?
- 15. 如何以編程方式獲取iPhone是否使用3G,4G或GPRS連接?有沒有辦法以編程方式啓用和禁用3G連接?
- 16. 如何以編程方式檢查Android中藍牙設備的連接狀態?
- 17. 以編程方式使用藍牙連接兩個Android設備
- 18. 以編程方式在Google Android中連接到WiFi?
- 19. 以編程方式在Android中建立WPS連接?
- 20. 如何以編程方式禁用/啓用Android中的物理鍵盤?
- 21. 如何使用C#以編程方式連接到VSS?
- 22. 如何以編程方式在C#中連接ToolStripButton事件?
- 23. 如何以編程方式在WM中「通過藍牙連接」?
- 24. 如何以編程方式在ASP.Net中連接LINQ數據源?
- 25. 如何以編程方式在C#中連接到SQL Server?
- 26. 如何以編程方式禁用iis中的Etags 6
- 27. 以編程方式連接兩個UIButtons
+1很好的解決方案 –
當你將手機切換到飛行模式也將抑制的Wi-Fi和藍牙。我不認爲這是預期的。主要任務是僅抑制GSM連接... – barmaley
yes.i已經嘗試過飛行模式。這將禁用藍牙也。但我想禁用只有通話和短信是可能的?????我禁用GPRS。有什麼方法可用嗎? – Ram