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

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

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比青山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式青山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋青山地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

我們在處理某個(gè)Wifi連接時(shí),有時(shí)會(huì)需要忘掉當(dāng)前連接的密碼信息。執(zhí)行這項(xiàng)操作,我們需要調(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ò)連接的配置信息就會(huì)從wpa_supplicant.conf中刪掉;之后這個(gè)網(wǎng)絡(luò)就不會(huì)有自動(dòng)重連的動(dòng)作,因?yàn)閏onf文件中已經(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。此時(shí)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文件的配置信息并進(jìn)行保存;執(zhí)行完成后,forget()函數(shù)結(jié)束了。通過代碼我們發(fā)現(xiàn),執(zhí)行forget()函數(shù)并不會(huì)引起WifiStateMachine中狀態(tài)的切換。

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

新聞名稱:Android  Wifi的forget()操作實(shí)例詳解
標(biāo)題來源:http://muchs.cn/article40/ijsgeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、營銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、微信小程序自適應(yīng)網(wǎng)站、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司