2012-05-10 211 views
1

我可以通過它檢查設備是否連接到wifi。如何查看設備連接的wifi網絡連接速度?

public boolean isOnline() { 
    ConnectivityManager cm = (ConnectivityManager) activity 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 

    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false; 
} 

但是,我想檢查ping的無線連接速度。我想基於這個ping數來設置一些變量。像這樣的,

public int internetspeed(){ 
    checking... 
    return speed; 
} 

有人可以給我教程或例子嗎?

+0

這可能會幫助您提供您http://stackoverflow.com/questions/2786720/android-service-ping-url – moujib

回答

2

這個片段將做的工作適合你

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE); 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    if (wifiInfo != null) { 
     Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS 
    } 
0

在後臺下載文件並測量每秒的數據。

0

這是我用來測試連接性的完整類。對於ping的問題,請參閱我的評論。

public class InternetCheck { 

    Context mContext; 
    public InternetCheck(Context mContext){ 
      this.mContext = mContext; 
     } 


    // CHECK FOR INTERNET METHOD 
    public final boolean isInternetOn() { 
     ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
     // ARE WE CONNECTED TO THE NET 
     if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
       || connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING 
       || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING 
       || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) { 
      return true; 
     } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
       || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) { 
      return false; 
     } 
     return false; 
    } 

} 
+0

鏈接是ping一個url,但我不知道設備連接到路由器的ip地址,所以不能ping也 –

相關問題