【问题标题】:Disable flight mode in android 2.2在 android 2.2 中禁用飞行模式
【发布时间】:2023-03-08 09:57:01
【问题描述】:

我正在使用一个允许启用飞行模式的 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();

                }

【问题讨论】:

    标签: android offline-mode


    【解决方案1】:

    您的代码看起来不错。确保您的清单包含:

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

    并且系统设置应保持您的应用设置它们的方式。

    【讨论】:

      【解决方案2】:

      我在我的一个应用中实现了完全相同的功能。

      您是在问政策应该是什么?这是我所做的:当我的应用程序启动时,它会记录当前的飞行模式并相应地存储一个标志。如果设备已经处于飞行模式,它什么也不做。否则进入飞行模式。

      当应用程序退出时,它会检查它存储的标志,以查看设备在启动时是否处于飞行模式,在这种情况下它什么也不做。如果该标志表明设备尚未处于飞行模式,则它现在会重新关闭飞行模式。

      启发式方法并不完美——如果系统杀死了我的应用程序,然后我重新启动它,重新启动的实例将观察到设备处于飞行模式,但不会记住它之前没有处于飞行模式。

      代码大致如下:

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

      一些细节留给读者练习。

      【讨论】:

      • 该应用程序实际上是在飞机上使用的。当 GPS 指示设备的速度足以飞行时,它会进入飞行模式。当 gps 指示设备再次停止时,它会离开飞行模式。然而,这就是所有的实现细节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多