回答
控制飛行模式
檢查,看它是否被啓用與否:
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
要切換它:
// 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);
而且,您可以使用此功能:
/* 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
更多詳情:
http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html
編輯:我看,這種方法是不再可能使用Android 4.2及以上。但我認爲,你可以閱讀一些有趣的代碼,在工作:
How to turn on/off airplane mode, even on new Android versions (and even with root)?
我在之前看到過帖子,但是它給出了'Permission Denial:不允許發送廣播android.intent.action.AIRPLANE_MODE'異常,我是否需要root權限才能使用該代碼? – user2654072
@ user2654072正如我在答案中所說的,您需要您在manifest.xml中需要的權限'android.permission.WRITE_SETTINGS'這個<使用權限android:name =「android.permission.WRITE_SETTINGS」/>' –
@ user2654072我編輯答案,看看答案的最後。 –
- 1. 在Android的web視圖4.4
- 2. Android 4.4系統+ Parse.com指針
- 3. 材料上Android 4.4的
- 4. 文件上傳Android 4.4 WebView
- 5. 在Android 4.4中啓用TLS 1.2
- 6. 資源$ Android Android 4.4上的NotFoundException
- 7. 在contao 4.4
- 8. 在Android-4.4上檢測到Web Sql語法錯誤但未低於4.4版本
- 9. memdiacodec解碼器工作在android 5.0但不是在Android 4.4
- 10. OpenCV的在Xcode 4.4
- 11. Android 4.4以上版本重啓
- 12. Android 4.4支持SHA256密碼嗎?
- 13. HttpURLConnection的不斷的Android 4.4系統
- 14. Android 4.4從CardView中刪除陰影
- 15. StyleCopCmd:用StyleCop 4.4運行StyleCopCmd 4.4。*
- 16. 在../Android/data中創建包目錄在Android上的SD卡上拒絕4.4
- 17. 連接錯誤在Xcode 4.4
- 18. 在Qt 4.4中使用QMDIArea。
- 19. Ocropus 4.4。崩潰在Xubuntu 10.4
- 20. WebView:WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING:在Andoird 4.4下面?
- 21. gcc 4.4 in Xcode
- 22. Xcode 4.4與admod
- 23. 角4.4 - ExpressionChangedAfterItHasBeenCheckedError
- 24. Android 4.4。在VirtualBox下無法訪問ACR122U閱讀器
- 25. 在Android 4.4及更高版本上編寫外部存儲
- 26. 在WebView的Android 4.4中的jQuery的概率
- 27. Inflate NavigationView在Android 4.4及更低版本中導致異常
- 28. 我們如何在Android上的HttpClient 4.4+中啓用SNI?
- 29. 如何在Android 4.4版上設置漣漪效應?
- 30. Xamarin.Android Android 4.4(API 19)在啓動時引發錯誤使用Xamarin.Forms
「飛行模式很難用」怎麼難用? – m0skit0