0

I have written code to ON/OFF AirPlane/Flight mode programmatically, and still i am using two different buttons to control that, one to ON Airplane mode and second to OFF Airplane mode, using below code:

@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

    }

But now i want to know the status of Airplane mode in every 2 seconds for that i have written timer code, and if Airplane mode is ON i want to turn OFF it automatically:

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

    }

So what i have to do, to turn off airplane mode without clicking on button ?

Sophie
  • 2,594
  • 10
  • 41
  • 75
  • take a boolean variable which will be true when airplane mode is on and make airplane mode off when its false. (Get the difference in the two part of the sentence) and then in a function when the variable is true make it false. (So airplane mode is off) – therealprashant Oct 21 '14 at 10:20

3 Answers3

2

Just call your airPlanemodeOFF function in the run method of your timertask.

You don't need to provide a view for it. The method don't use it, you can pass null as a parameter. I guess you linked the button with the xml feature, the view is a parameter because you could link the same function to multiple button and check which one called it.

Xavier Falempin
  • 1,186
  • 7
  • 19
1

Try this Function it returns Boolean value whether airplane mode is on or off

private static boolean isAirplaneModeOn(Context context) {

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

}
Amy
  • 4,034
  • 1
  • 20
  • 34
0

You don't need to have a TimerTask to check the status every 2 seconds, you can add a broadcast receiver, and listen to "android.intent.action.AIRPLANE_MODE" action.

<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
   }    
}
JC1129
  • 1
  • 1