在Android 2.2中禁用飞行模式

时间:2013-05-10 06:00:36

标签: android offline-mode

我正在使用允许启用飞行模式的Android应用。使能部分工作正常。现在,当用户存在/退出应用程序时出现问题。我希望用户退出应用后禁用航班模式。是否有可能以编程方式进行,或者用户是否应手动设置?

 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();

                }

2 个答案:

答案 0 :(得分:0)

您的代码看起来很好。确保您的清单包含:

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

并且系统设置应该与您的应用设置方式保持一致。

答案 1 :(得分: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);
        }
    }
}

一些细节留给读者练习。