如果开启,则自动关闭飞机/飞行模式

时间:2014-10-21 10:04:20

标签: android timer settings activity-lifecycle airplane

我已经以编程方式编写了代码to ON/OFF AirPlane/Flight mode,但仍然使用two个不同的buttons来控制它,一个用于开启飞行模式,第二个用于< strong> 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

    }

但是现在我想知道每个status飞行模式2 seconds,因为我编写了计时器代码,如果是飞机模式开启我想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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

尝试此函数,无论飞行模式是开启还是关闭,它都会返回布尔值

private static boolean isAirplaneModeOn(Context context) {

   return Settings.System.getInt(context.getContentResolver(),
           Settings.System.AIRPLANE_MODE_ON, 0) != 0;

}

答案 2 :(得分:0)

你不需要每2秒检查一次TimerTask状态,你可以添加一个广播接收器,并听取&#34; android.intent.action.AIRPLANE_MODE&#34;动作。

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