1、介绍Settings.System
java.lang.Object | ||
↳ | android.provider.Settings.NameValueTable | |
↳ | android.provider.Settings.System |
Class Overview
System settings, containing miscellaneous system preferences. This table holds simple name/value pairs. There are convenience functions for accessing individual settings entries.
系统设置包含着各种各样的系统设置,这个表是以 key values的形式存储。
2、使用案例:
/**
* 在android中,每个应用程序是可以实现数据共享的,对于每一个应用程序程序都拥有一个c
* ontentprovider实例进行存储,而contentresolver则是用于管理所有程序的contentprovider
* 实例,通过contentrescolver可以获得数据,插入数据等……至于getcontentrescolver()就是获取实例。。。
*/
ContentResolver cr = getContentResolver();
if(android.provider.Settings.System.getString(cr,Settings.System.AIRPLANE_MODE_ON).equals("0")){
//获取当前飞行模式状态,返回的是String值0,或1.0为关闭飞行,1为开启飞行
//如果关闭飞行,则打开飞行
Settings.System.putString(cr,Settings.System.AIRPLANE_MODE_ON, "1");
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
sendBroadcast(intent);
}else{
//否则关闭飞行
Settings.System.putString(cr,Settings.System.AIRPLANE_MODE_ON, "0");
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
sendBroadcast(intent);
}
权限声明:<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
3、其他切换的详情
来源:http://www.eoeandroid.com/thread-97809-1-1.html
因为GSM ,蓝牙,wifi模块分别注册了对 ACTION_AIRPLANE_MODE_CHANGED 消息的监测,所以收到
该消息后,模块会进行切换。
BluetoothDeviceService.java
开启蓝牙:enable(false);
关闭蓝牙:disable(false);
PhoneApp.java (packages\apps\phone\src\com\android\phone)
设置GSM模块状态 phone.setRadioPower(enabled);
WifiService.java
设置 wifi 状态 setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
GSM模块切换过程分析:
phone.setRadioPower(enabled)调用的是:
文件 GSMPhone.java 中的的函数:
public void setRadioPower(boolean power)
mSST.setRadioPower(power);
因为有 ServiceStateTracker mSST;
mSST.setRadioPower 调用的是文件 ServiceStateTracker.java 中的函数:
public void setRadioPower(boolean power)
mDesiredPowerState = power;
setPowerStateToDesired();
cm.setRadioPower(true, null);
或者
cm.setRadioPower(false, null);
因为有:
CommandsInterface cm;
public final class RIL extends BaseCommands implements CommandsInterface
所以 cm.setRadioPower 调用的是文件 RIL.java 中的函数:
public void setRadioPower(boolean on, Message result)
RILRequest rr = RILRequest.obtain(RIL_REQUEST_RADIO_POWER, result);
rr.mp.writeInt(1);
...
send(rr)
通过 send 向 rild 发送 RIL_REQUEST_RADIO_POWER 请求来开启或者关闭GSM模块。
rild 数据接收流程:
收到 RIL_REQUEST_RADIO_POWER 执行:
requestRadioPower(data, datalen, t);
然后根据条件往无线模块发送模块开启和关闭请求
主要的at命令有:
err = at_send_command("AT+CFUN=0", &p_response);
err = at_send_command("AT+CFUN=1", &p_response);
蓝牙模块切换过程分析:
enable(false);
蓝牙开启调用文件 BluetoothDeviceService.java 中的函数:
public synchronized boolean enable(boolean saveSetting)
setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_ON);
mEnableThread = new EnableThread(saveSetting);
mEnableThread.start();
disable(false)
蓝牙关闭调用文件 中的函数:
public synchronized boolean disable(boolean saveSetting)
setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF);
wifi模块切换过程分析:
广播 wifi 状态改变的消息:WIFI_STATE_CHANGED_ACTION
setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING, uid);
更新 wifi 状态:
private void updateWifiState()
如果需要使能开启 wifi 那么会发送:
sendEnableMessage(true, false, mLastEnableUid);
sendStartMessage(strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
消息循环中处理命令消息:
public void handleMessage(Message msg)
如果使能wifi:setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
开启wifi: mWifiStateTracker.setScanOnlyMode(msg.arg1 != 0);
setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
断开 mWifiStateTracker.disconnectAndStop();
开启过程步骤:
1> 装载 wifi 驱动: WifiNative.loadDriver()
2> 启动后退 daemo supplicant: WifiNative.startSupplicant()
关闭过程步骤:
1> 停止后退 daemo supplicant:WifiNative.stopSupplicant()
2> 卸载 wifi 驱动: WifiNative.unloadDriver()
如果 wifi 状态默认为开启那么 WifiService 服务的构造函数:
WifiService(Context context, WifiStateTracker tracker)
boolean wifiEnabled = getPersistedWifiEnabled();
setWifiEnabledBlocking(wifiEnabled, false, Process.myUid());
会开启wifi模块。