欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Wifi的forget()操作实例详解

程序员文章站 2023-12-19 17:33:22
android  wifi的forget()操作实例详解 我们在处理某个wifi连接时,有时会需要忘掉当前连接的密码信息。执行这项操作,我们需要调用wifima...

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, 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; 
  } 
} 

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

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

上一篇:

下一篇: