[英]Android: How to detect if a device is WiFi or WiFi+Cellular
有沒有一種方法可以檢查用戶是否正在使用帶有蜂窩連接的設備(這主要適用於平板電腦)? 也就是說,智能手機帶有內置的Wi-Fi和Cellular(通常),但是某些平板電腦僅帶有Wi-Fi。 我如何知道運行我的應用程序的設備類型?
我嘗試了以下無結果的方法:
cell = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE);
wifi = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_WIFI);
if (cell) tv_1.setText("The tablet has cellular");
else tv_1.setText("The tablet does not have cellular");
if (wifi) tv_2.setText("The tablet has wifi");
else tv_2.setText("The tablet does not have wifi");
問題在於,即使這是一台沒有手機的平板電腦,兩個比較總是返回true。
我只需要知道設備是否具有SIM卡插槽(帶蜂窩的型號)或僅具有WiFi的型號,可以嗎?
提前致謝。
如果目標是確定是否對連接進行計量,則應調用ConnectivityManager.isActiveNetworkMetered() ;如果需要較早的設備支持,則應調用ConnectivityManagerCompat.isActiveNetworkMetered() 。
有關對各種連接類型做出反應的背景信息,請參閱《 管理網絡使用情況》 (盡管請注意該文檔的問題是不使用isActiveNetworkMetered() )。
這是我的代碼的摘錄(到目前為止有效):
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mWifi!=null) isOnWifi = mWifi.isConnected();
if (mEthernet!=null) isOnEthernet = mEthernet.isConnected();
if (m3G!=null) is3G = m3G.isConnected();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.