ConnectivityManager ConnectivityService in Android
生活随笔
收集整理的這篇文章主要介紹了
ConnectivityManager ConnectivityService in Android
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?connectivity manager (封裝了ConnectivityService)
管理多種連接方式 mobile/wifi/ether/bt/usb-tether/etc ? 要新加一個type,需要很復雜的改動(patch) 同一時刻只有一個active的數(shù)據(jù)連接 見NetworkInfo ?getActiveNetworkInfo() ?(也就是說不支持3G wifi同時開著) ? ? (但是,將來很可能會改成支持多個網(wǎng)絡同時連接,見: private static class RadioAttributes { public int mSimultaneity; ... ) connection type的優(yōu)先順序? ConncectionManager.java 提供了getNetworkPreference setNetworkPreference方法 preference的網(wǎng)絡可以是任何type,比如mobile, wifi, bt都可以。 ? 目前preference網(wǎng)絡只能有一個 ?也就是不支持優(yōu)先級列表,比如最prefer wifi,其次prefer ether,再次prefer mobile 3G (但是,將來很可能改成支持優(yōu)先列表,見: ? ? // priority order of the nettrackers // (excluding dynamically set mNetworkPreference) // TODO - move mNetworkTypePreference into this private int[] mPriorityList;、 ) 當調(diào)用setNetworkPreference時,perfer的網(wǎng)絡會被持久化,記錄在數(shù)據(jù)庫中 當調(diào)用getNetworkPreference時,會從數(shù)據(jù)庫中取回,如果沒有記錄,就默認wifi (詳見ConnectivityManager.java的hard code : public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;) 這里,只是說優(yōu)先wifi連接模式,不能設置哪個AP/SSID優(yōu)先 (AP/SSID優(yōu)先也能做,另有N種實現(xiàn)方法,都不難) 已經(jīng)保存了優(yōu)先網(wǎng)絡,如何enforcePreference? todo 當WiFi連上,已經(jīng)存在的連接(e.g. mobile GPRS,3G,etc)會自動斷開 (因為同時不能存在兩個active連接) 當離開wifi熱點,信號不足時,wifi斷開,如仍有數(shù)據(jù)連接請求,會嘗試使用其它連接,如mobile。 這些邏輯保存在ConnectivityService.java中handleConnected handleDisconnect等方法中,未深入研究 network config, radio attribute等配置文件在 /frameworks/base/core/res/res/values/config.xml (也可針對device定制config.xml,保存在device目錄中)? 通過讀這個文件初始化ConnectivityService的 mNetConfigs mRadioAttributes,見下面xml comments 107 ? ? <!-- An Array of "[Connection name],[ConnectivityManager.TYPE_xxxx], 108 ? ? ? ? ?[associated radio-type],[priority],[restoral-timer(ms)],[dependencyMet] ?--> 109 ? ? <!-- the 5th element "resore-time" indicates the number of milliseconds to delay 110 ? ? ? ? ?before automatically restore the default connection. ?Set -1 if the connection 111 ? ? ? ? ?does not require auto-restore. --> 112 ? ? <!-- the 6th element indicates boot-time dependency-met value. --> 113 ? ? <string-array translatable="false" name="networkAttributes"> 114 ? ? ? ? <item>"wifi,1,1,1,-1,true"</item> 115 ? ? ? ? <item>"mobile,0,0,0,-1,true"</item> 116 ? ? ? ? <item>"mobile_mms,2,0,2,60000,true"</item> 117 ? ? ? ? <item>"mobile_supl,3,0,2,60000,true"</item> 118 ? ? ? ? <item>"mobile_hipri,5,0,3,60000,true"</item> 119 ? ? ? ? <item>"mobile_fota,10,0,2,60000,true"</item> 120 ? ? ? ? <item>"mobile_ims,11,0,2,60000,true"</item> 121 ? ? ? ? <item>"mobile_cbs,12,0,2,60000,true"</item> 122 ? ? ? ? <item>"wifi_p2p,13,1,0,-1,true"</item> 123 ? ? </string-array> ? ? ... 138 ? ? <string-array translatable="false" name="radioAttributes"> 139 ? ? ? ? <item>"1,1"</item> 140 ? ? ? ? <item>"0,1"</item> 141 ? ? </string-array> 另外,ConnectivityService與DNS,×××,Tether,Proxy相關(guān)的代碼,均未深入研究 ConnectivityManager提供了抽象的連接管理功能,與具體連接物理層無關(guān), 例如wifi的鏈接管理,需要看 wifi manager/ wifi service , which I did not investigate yet.?轉(zhuǎn)載于:https://blog.51cto.com/mengxx/751198
總結(jié)
以上是生活随笔為你收集整理的ConnectivityManager ConnectivityService in Android的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]在Visual C++中以错误的顺
- 下一篇: 安装UBUNTU Server 11.1