Call Android API to control flight mode (Airplane Mode) On / Off


点击查看原文 

注意: 源代码中,部分 ( ) 应为 { }


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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值