2010-07-14 58 views
4

我打算放棄呼叫,我覺得這是解決方法之一。 如何通過代碼激活飛行模式?如何將AIRPLANE_MODE_ON設置爲「True」或ON?

這樣我會根據某些事件放棄呼叫。

+2

請注意,開/關切換飛行模式不再可能由於Android 4.2(果凍豆主要版本1)的。這是因爲Google採用了BAN​​DAGE解決方案來解決Android中的缺陷,而不是重新檢查其安全模型。 – ChuongPham 2012-11-24 17:07:05

回答

15

查看博客文章Android: Controlling Airplane Mode

只能高達API 16

// 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是飛行模式是否啓用。

+0

我無法部署它。此代碼是否在Broadcastreceiver類 – 2010-07-14 21:18:48

+1

中發佈?無論您需要執行此操作,它都可以使用。不過請記住,您需要WRITE_SETTINGS權限來執行此操作。 – Jess 2010-07-15 05:08:28

+0

我仍然收到像Settings.System錯誤\t解決 – 2010-07-15 21:01:12

0
public static boolean getAirplaneMode(Context context) { 
     try { 
     int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); 
     return airplaneModeSetting==1?true:false; 
    } catch (SettingNotFoundException e) { 
     return false; 
    } 
    } 
相關問題