2013-05-10 69 views
0

我正在使用允許啓用飛行模式的Android應用程序。啓用部分工作正常。現在問題出現在用戶存在/退出應用程序時。我想要在用戶退出應用程序後禁用飛行模式。是否有可能以編程方式進行編輯或者用戶是否需要手動轉動設置?在Android 2.2中禁用飛行模式

if ((flightMode.isChecked())) 
    { 

      boolean isEnabled = Settings.System.getInt(getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON, 0) == 1; 
      flag=1; 
      Toast.makeText(this, "flight mode on", Toast.LENGTH_SHORT).show(); 
      if(isEnabled == false) 
      { 

      Settings.System.putInt(getContentResolver(), 
        Settings.System.AIRPLANE_MODE_ON,1); 

      Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
      intent.putExtra("state", 1); 
      sendBroadcast(intent); 


      } 

      } 
     else 
     { 
      Settings.System.putInt(getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON,0); 
      Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
      intent.putExtra("state", 0); 
      sendBroadcast(intent); 
      Toast.makeText(this, "flight mode off", Toast.LENGTH_SHORT).show(); 

     } 

並禁用我用這個代碼:

     alt_bld .setMessage("Are you sure you want to exit?") 
         alt_bld .setCancelable(true); 
       alt_bld.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
       { 

         public void onClick(DialogInterface dialog,int id) 
       { 

       if(flag==1) 
       { 
        Settings.System.putInt(getContentResolver(), 
        Settings.System.AIRPLANE_MODE_ON,0); 
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
             intent.putExtra("state", 0); 
             sendBroadcast(intent); 


          } 
          finish(); 

       } 

回答

0

您的代碼看起來不錯。確保您的清單包含:

<uses-permission android:name="WRITE_SETTINGS" /> 

並且系統設置應該保持您的應用設置它們的方式。

+0

我已經包括了.. – Sindu 2013-05-10 06:08:25

0

我在其中一個應用中實現了完全相同的功能。

你在問這個政策應該是什麼?以下是我所做的:當我的應用程序啓動時,它會記下當前的飛行模式並相應地存儲標誌。如果設備已經處於飛行模式,它什麼都不做。否則,它進入飛行模式。

當應用程序退出時,它會檢查它存儲的標記,以查看設備在啓動時是否處於飛行模式,在這種情況下,它什麼也不做。如果該標誌指示設備未處於飛行模式,則現在將飛行模式關閉。

啓發式不完美 - 如果系統殺死我的應用程序,並且稍後重新啓動它,則重新啓動的實例將觀察到設備處於飛行模式,但不會記得它以前未處於飛行模式。

代碼看起來大約是這樣的:

class MyClass extends Activity { 
    ... 
    boolean airplaneMode;    // currently-desired airplane mode 
    boolean oldAirplaneMode;   // airplane mode at startup 
    ... 
    onCreate(Bundle savedState) { 

     if (savedState != null) { 
      oldAirplaneMode = savedState.getBoolean("oldAirplaneMode"); 
      airplaneMode = state.getBoolean("airplaneMode"); 
     } 

     // Programming notes: On initial 
     // startup we note the initial airplane mode and take no other 
     // action. On subsequent entries to this method, we confirm that 
     // the current airplane mode is what we want, and set it if needed. 
     // 
     // If airplane mode was initially enabled when the app started, 
     // then we leave it that way. 
     // 
     // If for some reason, the program exits before the plane lands, then   
     // airplane mode will remain on. It is the user's responsibility to 
     // restore phone mode in that case. 

     boolean mode = getAirplaneMode(); 
     // First startup; remember initial value of airplane mode 
     if(savedState == null) 
      oldAirplaneMode = airplaneMode = mode; 
     else if(!oldAirplaneMode && mode != airplaneMode) 
      setAirplaneMode(airplaneMode); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle state) { 
     state.putBoolean("autoAirplaneMode", autoAirplaneMode); 
     state.putBoolean("oldAirplaneMode", oldAirplaneMode); 
     state.putBoolean("airplaneMode", airplaneMode); 
    } 

    private void eventYadaYada() { 
     // Should we be in airplane mode now? 
     boolean mode = decideIfWeShouldBeInAirplaneMode(); 
     if (!oldAirplaneMode) // Only act if we weren't in airplane mode at startup 
     { 
      if(airplaneMode != mode) 
       setAirplaneMode(airplaneMode = mode); 
     } 
    } 
} 

一些細節作爲一個練習留給讀者。

+0

但是,你在任何時候禁用模式? – Sindu 2013-05-10 07:23:25

+0

該應用程序字面上用於飛機。當GPS顯示設備正在快速飛行時,它會進入飛行模式。當gps指示設備再次停止時,它會離開飛行模式。然而,這是所有的實現細節。 – 2013-05-10 15:09:11