【问题标题】:Turn Off Airplane/Flight Mode automatically if ON如果打开,则自动关闭飞行/飞行模式
【发布时间】:2014-10-21 10:04:20
【问题描述】:

我以编程方式编写了代码to ON/OFF AirPlane/Flight mode,但我仍然使用two 不同的buttons 来控制它,一个是ON飞行模式,第二个是OFF 飞行模式,使用以下代码:

@SuppressWarnings("deprecation")
    public void airPlanemodeON(View v) {
        boolean isEnabled = Settings.System.getInt(this.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        if (isEnabled == false) { // means this is the request to turn OFF AIRPLANE mode
            modifyAirplanemode(true); // ON
            Toast.makeText(getApplicationContext(), "Airplane Mode ON",
                    Toast.LENGTH_LONG).show();
        }
    }

    @SuppressWarnings("deprecation")
    public void airPlanemodeOFF(View v) {
        boolean isEnabled = Settings.System.getInt(this.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        if (isEnabled == true) // means this is the request to turn ON AIRPLANE mode
        {
            modifyAirplanemode(false); // OFF
            Toast.makeText(getApplicationContext(), "Airplane Mode OFF",
                    Toast.LENGTH_LONG).show();
        }
    }

    @SuppressWarnings("deprecation")
    public void modifyAirplanemode(boolean mode) {
        Settings.System.putInt(getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, mode ? 1 : 0);// Turning ON/OFF Airplane mode.

        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);// creating intent and Specifying action for AIRPLANE mode.
        intent.putExtra("state", !mode);// indicate the "state" of airplane mode is changed to ON/OFF
        sendBroadcast(intent);// Broadcasting and Intent

    }

但现在我想知道每个2 seconds飞行模式status,因为我已经编写了 timer 代码,并且如果飞行模式打开我想turn OFF自动

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startTimer();
    }

@Override
    protected void onResume() {
        super.onResume();

        //onResume we start our timer so it can start when the app comes from the background
        startTimer();
    }

    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, after the first 1000ms the TimerTask will run every 2000ms
        timer.schedule(timerTask, 1000, 2000); //
    }

    public void stoptimertask(View v) {
        //stop the timer, if it's not already null
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {

                //use a handler to run a toast that shows the current timestamp
                handler.post(new Runnable() {
                    public void run() {

                    }
                });
            }
        };
    }

    /** Called when another activity is taking focus. */
    @Override
    protected void onPause() {
       super.onPause();
            //stop the timer, if it's not already null
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
    }

    /** Called when the activity is no longer visible. */
    @Override
    protected void onStop() {
       super.onStop();

    }

    /** Called just before the activity is destroyed. */
    @Override
    public void onDestroy() {
       super.onDestroy();

    }

那么我要做什么,打开off飞行模式without点击button

【问题讨论】:

  • 取一个布尔变量,当飞行模式开启时为真,当飞行模式为假时关闭飞行模式。 (获取句子两部分的区别)然后在一个函数中当变量为真时使其为假。 (所以飞行模式关闭)

标签: android timer settings activity-lifecycle airplane


【解决方案1】:

只需在 timertask 的 run 方法中调用 airPlanemodeOFF 函数即可。

您无需为其提供视图。该方法不使用它,您可以将 null 作为参数传递。 我猜您将按钮与 xml 功能链接在一起,视图是一个参数,因为您可以将相同的功能链接到多个按钮并检查哪个按钮调用了它。

【讨论】:

    【解决方案2】:

    试试这个函数,无论飞行模式是打开还是关闭,它都会返回布尔值

    private static boolean isAirplaneModeOn(Context context) {
    
       return Settings.System.getInt(context.getContentResolver(),
               Settings.System.AIRPLANE_MODE_ON, 0) != 0;
    
    }
    

    【讨论】:

      【解决方案3】:

      你不需要每2秒检查一次状态的TimerTask,你可以添加一个广播接收器,并监听“android.intent.action.AIRPLANE_MODE”动作。

      <receiver android:name="com.appname.AirplaneModeChangeReceiver">
       <intent-filter>
           <action android:name="android.intent.action.AIRPLANE_MODE"/>
       </intent-filter>
      </receiver>
      
      
      public class AirplaneModeChangeReceiver extends BroadcastReceiver {
            public void onReceive(Context context, Intent intent) {
      
            //check status, and turn it on/off here
         }    
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-14
        相关资源
        最近更新 更多