2013-05-10 68 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