当我们在手机的setting中打开wifi开关时,首先是wifisetting中的oncreate将加载布局文件。
@Override
public void onCreate (Bundle icicle) {
super .onCreate(icicle);
addPreferencesFromResource(R.xml.wifi_settings);
mAddPreference = new Preference(getContext());
mAddPreference.setIcon(R.drawable.ic_menu_add_inset);
mAddPreference.setTitle(R.string.wifi_add_network);
mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
mBgThread.start();
mWifiSettingsExt = new WifiSettingsExt(getActivity());
mWifiSettingsExt.onCreate();
}
接下来在onStart中将创建 WifiEnabler
@Override
public void onStart () {
super .onStart();
mWifiEnabler = createWifiEnabler();
}
/**
* @return new WifiEnabler or null (as overridden by WifiSettingsForSetupWizard)
*/
WifiEnabler createWifiEnabler() {
final SettingsActivity activity = (SettingsActivity) getActivity();
return new WifiEnabler(activity, activity.getSwitchBar());
}
WifiEnabler 构造函数中,将获得 WifiManager,并且添加广播监听事件,接下来显示 switchBar, 同时将注册switchBar的监听事件。
public WifiEnabler (Context context, SwitchBar switchBar) {
mContext = context;
mSwitchBar = switchBar;
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
setupSwitchBar();
}
public void setupSwitchBar () {
final int state = mWifiManager.getWifiState();
handleWifiStateChanged(state);
if (!mListeningToOnSwitchChange) {
mSwitchBar.addOnSwitchChangeListener(this );
mListeningToOnSwitchChange = true ;
}
mSwitchBar.show();
}
当我们打开wifi开关的时候,SwitchBar监听事件将触发。此时将执行mWifiManager.setWifiEnabled(isChecked)。
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
Log . d(TAG , "onCheckedChanged, isChecked = " + isChecked);
if (mStateMachineEvent) {
return ;
}
if (isChecked && ! WirelessUtils. isRadioAllowed(mContext, Settings. Global . RADIO_WIFI)) {
Toast. makeText(mContext, R. string . wifi_in_airplane_mode, Toast. LENGTH_SHORT). show();
mSwitchBar. setChecked(false );
return ;
}
if (mayDisableTethering(isChecked)) {
mWifiManager. setWifiApEnabled(null , false );
}
Log . d(TAG , "onCheckedChanged, setWifiEnabled = " + isChecked);
MetricsLogger. action(mContext,
isChecked ? MetricsEvent. ACTION_WIFI_ON : MetricsEvent. ACTION_WIFI_OFF);
if (! mWifiManager. setWifiEnabled(isChecked)) {
mSwitchBar. setEnabled(true );
Toast. makeText(mContext, R. string . wifi_error, Toast. LENGTH_SHORT). show();
}
}
WifiManager中将直接调用 WifiServiceImpl中的同名函数,此时WifiManager与WifiService将通过adil进行异步通信。
/**
* Enable or disable Wi-Fi.
* @param enabled {@code true} to enable, {@code false} to disable.
* @return {@code true} if the operation succeeds (or if the existing state
* is the same as the requested state).
*/
public boolean setWifiEnabled (boolean enabled) {
try {
return mService.setWifiEnabled(enabled);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
WifiServiceImpl 中的setWifiEnabled 将向 WifiController 中发送 CMD_WIFI_TOGGLED 消息。
/**
* see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
* @param enable {@code true} to enable, {@code false} to disable.
* @return {@code true} if the enable/disable operation was
* started or is already in the queue.
*/
public synchronized boolean setWifiEnabled (boolean enable) {
enforceChangePermission();