注意: 源代码中,部分 ( ) 应为 { }
If the phone is in Flight Mode (Airplane Mode) in the case, then all input and output signals will be stopped and closed, such as Bluetooth (Bluetooth) and WIFI and so on. If the night do not want people calling to bother, but want the phone to the normal execution of other programs, then this model may be considered.
Suppose I write an alarm clock program that allows users to cut the opening into the phone to airplane mode to prevent nuisance calls, wait time to get up after the flight mode to turn off automatically. Then if we can control program in the free flight mode switch, then it will be more convenient to use.
We can use android.provider.Settings.System API provided to access the system settings. For example I would like to know whether flight mode enabled, then:
View source Android
import android.content.Context;
import android.provider.Settings;
public static boolean isAirplaneModeOn (Context context) (
return Settings.System.getInt (context.getContentResolver (), Settings.System.AIRPLANE_MODE_ON, 0)! = 0;
)
Settings.System.AIRPLANE_MODE_ON that we are to make the project, to obtain other words can refer to android.provider.Settings.System the constant list.
To set flight mode switch is the same as with android.provider.Settings.System about:
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
public static void setAirplaneMode (Context context, boolean status) (
/ / First determine whether the current is already open Flight mode
boolean isAirplaneModeOn = isAirplaneModeOn (context);
if ((status & & isAirplaneModeOn) | | (! status & &! isAirplaneModeOn)) (
return;
)
int mode = status? 1: 0;
/ / Set the flight mode, and broadcast out of the state
Settings.System.putInt (context.getContentResolver (), Settings.System.AIRPLANE_MODE_ON, mode);
Intent i = new Intent (Intent.ACTION_AIRPLANE_MODE_CHANGED);
i.putExtra ("state", mode);
context.sendBroadcast (i);
)
Note that, change system settings are required android.permission.WRITE_SETTINGS rights Caixing, so remember to add in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
As long as can write again with android.appwidget.AppWidgetProvider flight mode switching AppWidget tool La. But the problem was that, if the phone has set the boot with a PIN code, then close the flight mode it will need to enter the PIN code Caixing.