0

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

@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 secondsturn 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模式?withoutbutton

4

3 回答 3

2

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

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

于 2014-10-21T10:10:40.210 回答
1

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

private static boolean isAirplaneModeOn(Context context) {

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

}
于 2014-10-21T10:07:23.623 回答
0

您不需要每 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
   }    
}
于 2014-10-21T10:27:01.527 回答