国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Wifi的forget()操作實例詳解

2019-12-12 03:41:26
字體:
供稿:網(wǎng)友

Android  Wifi的forget()操作實例詳解

我們在處理某個Wifi連接時,有時會需要忘掉當(dāng)前連接的密碼信息。執(zhí)行這項操作,我們需要調(diào)用WifiManager::forget()函數(shù):

/**  * Delete the network in the supplicant config.  *  * This function is used instead of a sequence of removeNetwork()  * and saveConfiguration().  *  * @param config the set of variables that describe the configuration,  *      contained in a {@link WifiConfiguration} object.  * @param listener for callbacks on success or failure. Can be null.  * @throws IllegalStateException if the WifiManager instance needs to be  * initialized again  * @hide  */ public void forget(int netId, ActionListener listener) {   if (netId < 0) throw new IllegalArgumentException("Network id cannot be negative");   validateChannel();   sAsyncChannel.sendMessage(FORGET_NETWORK, netId, putListener(listener)); } 

從函數(shù)介紹可知,調(diào)用forget()函數(shù),當(dāng)前網(wǎng)絡(luò)連接的配置信息就會從wpa_supplicant.conf中刪掉;之后這個網(wǎng)絡(luò)就不會有自動重連的動作,因為conf文件中已經(jīng)沒有該網(wǎng)絡(luò)的配置信息。

跟蹤FORGET_NETWORK消息,WifiServiceImpl::ClientHandler處理:

case WifiManager.FORGET_NETWORK:   if (isOwner(msg.sendingUid)) {     mWifiStateMachine.sendMessage(Message.obtain(msg));   } else {     Slog.e(TAG, "Forget is not authorized for user");     replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED,         WifiManager.NOT_AUTHORIZED);   }   break; 

簡單地將該消息轉(zhuǎn)發(fā)給WifiStateMachine。此時Wifi是連接狀態(tài),WifiStateMachine中當(dāng)前狀態(tài)是ConnectedState,它的父狀態(tài)ConnectModeState處理:

case WifiManager.FORGET_NETWORK:   // Debug only, remember last configuration that was forgotten   WifiConfiguration toRemove       = mWifiConfigStore.getWifiConfiguration(message.arg1);   if (toRemove == null) {     lastForgetConfigurationAttempt = null;   } else {     lastForgetConfigurationAttempt = new WifiConfiguration(toRemove);   }   // check that the caller owns this network   netId = message.arg1;    if (!mWifiConfigStore.canModifyNetwork(message.sendingUid, netId,       /* onlyAnnotate */ false)) {     logw("Not authorized to forget network "        + " cnid=" + netId        + " uid=" + message.sendingUid);     replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED,         WifiManager.NOT_AUTHORIZED);     break;   }    if (mWifiConfigStore.forgetNetwork(message.arg1)) {     replyToMessage(message, WifiManager.FORGET_NETWORK_SUCCEEDED);     broadcastWifiCredentialChanged(WifiManager.WIFI_CREDENTIAL_FORGOT,         (WifiConfiguration) message.obj);   } else {     loge("Failed to forget network");     replyToMessage(message, WifiManager.FORGET_NETWORK_FAILED,         WifiManager.ERROR);   }   break; 

mWifiConfigStore.forgetNetwork():

/**  * Forget the specified network and save config  *  * @param netId network to forget  * @return {@code true} if it succeeds, {@code false} otherwise  */ boolean forgetNetwork(int netId) {   if (showNetworks) localLog("forgetNetwork", netId);    WifiConfiguration config = mConfiguredNetworks.get(netId);   boolean remove = removeConfigAndSendBroadcastIfNeeded(netId);   if (!remove) {     //success but we dont want to remove the network from supplicant conf file     return true;   }   if (mWifiNative.removeNetwork(netId)) {     if (config != null && config.isPasspoint()) {       writePasspointConfigs(config.FQDN, null);     }     mWifiNative.saveConfig();     writeKnownNetworkHistory(true);     return true;   } else {     loge("Failed to remove network " + netId);     return false;   } } 

根據(jù)傳入的當(dāng)前網(wǎng)絡(luò)的netId,分別調(diào)用WifiNative的removeNetwork()、saveConfig()方法刪除conf文件的配置信息并進行保存;執(zhí)行完成后,forget()函數(shù)結(jié)束了。通過代碼我們發(fā)現(xiàn),執(zhí)行forget()函數(shù)并不會引起WifiStateMachine中狀態(tài)的切換。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 冀州市| 鄯善县| 勐海县| 永春县| 加查县| 晋宁县| 延庆县| 健康| 香港| 上思县| 星子县| 商城县| 合川市| 新疆| 盖州市| 黔东| 西和县| 安宁市| 伊宁县| 财经| 绍兴县| 贡山| 东乡| 龙泉市| 灵川县| 涟源市| 平利县| 石嘴山市| 文水县| 苏尼特左旗| 惠来县| 棋牌| 顺昌县| 阿图什市| 明水县| 商洛市| 太保市| 油尖旺区| 彭泽县| 闽侯县| 柯坪县|