AndroidWifi的forget() *** 作实例详解

AndroidWifi的forget() *** 作实例详解,第1张

概述Android Wifi的forget() *** 作实例详解我们在处理某个Wifi连接时,有时会需要忘掉当前连接的密码信息。执行这项 *** 作,我们需要调用WifiManager::forget()函数:

AndroID  Wifi的forget() *** 作实例详解

我们在处理某个Wifi连接时,有时会需要忘掉当前连接的密码信息。执行这项 *** 作,我们需要调用WifiManager::forget()函数:

/**  * 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)); } 

从函数介绍可知,调用forget()函数,当前网络连接的配置信息就会从wpa_supplicant.conf中删掉;之后这个网络就不会有自动重连的动作,因为conf文件中已经没有该网络的配置信息。

跟踪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; 

简单地将该消息转发给WifiStateMachine。此时Wifi是连接状态,WifiStateMachine中当前状态是ConnectedState,它的父状态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,/* onlyAnnotate */ false)) {     logw("Not authorized to forget network "        + " cnID=" + netID        + " uID=" + message.sendingUID);     replyToMessage(message,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.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;   } } 

根据传入的当前网络的netID,分别调用WifiNative的removeNetwork()、saveConfig()方法删除conf文件的配置信息并进行保存;执行完成后,forget()函数结束了。通过代码我们发现,执行forget()函数并不会引起WifiStateMachine中状态的切换。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android Wifi的forget() *** 作实例详解全部内容,希望文章能够帮你解决Android Wifi的forget() *** 作实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146976.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存