我正在开发一个使用互联网连接工作的Android应用程序。我希望使用red for poor
连接,yellow for fair
和green for good
强度的指标来监控网络速度,并在状态栏上显示它,以便用户在使用app时可以随时了解网络速度。我遇到了TrafficStats库,我将获得no。使用TrafficStats.getMobileTxbytes()
传输的字节数和否。使用TrafficStats.getMobileRxbytes()
收到的字节数,但现在我有一个要处理的查询。
我只是Android的新手,想要一些见解。
还有其他一些解决这个问题的好方法吗?
答案 0 :(得分:0)
不幸的是,这些数据只能估算。 Android中没有API,可以在指定的时间内为您提供平均速度。
这是我根据移动网络连接类型(以及外部功能中的单位)指定平均速度所做的:
public float mobileNetSpeed(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = tm.getNetworkType();
float netSpeed = getMobileNetworkSpeed(networkType);
return netSpeed;
}
private Network.NetworkSpeedUnits getMobileNetworkSpeedUnit(int networkType) {
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case 16: // TelephonyManager.NETWORK_TYPE_GSM:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
case TelephonyManager.NETWORK_TYPE_UMTS:
return Network.NetworkSpeedUnits.KBps;
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
case TelephonyManager.NETWORK_TYPE_LTE:
case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
return Network.NetworkSpeedUnits.MBps;
default:
return Network.NetworkSpeedUnits.KBps;
}
}
/**
* Return hypothetical speed of mobile network. This method is an equivalent
* of {@link TelephonyManager#getNetworkClass()}
*
* @param networkType
* @return network speed by one of the XG type
*/
private float getMobileNetworkSpeed(int networkType) {
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
return 114;
case 16: // TelephonyManager.NETWORK_TYPE_GSM:
return 0;
case TelephonyManager.NETWORK_TYPE_EDGE:
return 296;
case TelephonyManager.NETWORK_TYPE_CDMA:
return 115;
case TelephonyManager.NETWORK_TYPE_1xRTT:
return 153;
case TelephonyManager.NETWORK_TYPE_IDEN:
return 60;
case TelephonyManager.NETWORK_TYPE_UMTS:
return 384;
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return 2.46F;
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return 3.1F;
case TelephonyManager.NETWORK_TYPE_HSDPA:
return 21.6F;
case TelephonyManager.NETWORK_TYPE_HSUPA:
return 5.76F;
case TelephonyManager.NETWORK_TYPE_HSPA:
return 14;
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return 4.9F;
case TelephonyManager.NETWORK_TYPE_EHRPD:
return 1.285F;
case TelephonyManager.NETWORK_TYPE_HSPAP:
return 42;
case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return 0;
case TelephonyManager.NETWORK_TYPE_LTE:
return 100;
case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
return 0;
default:
return 0;
}
}
但是,上面的代码仅适用于移动连接。当WiFi打开时,方法会有所不同:
public float getWiFiSpeed(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
return getWifiNetworkSpeed(wifiInfo);
}
/**
* Return general class of wifi network type. Unfortunately, there is no Android API method
* to do this, link speed in {@link WifiInfo#LINK_SPEED_UNITS "Mbps"} must be used
* and a maximum speed of wifi class must be compared with the value returned from
* {@link WifiInfo#getLinkSpeed()}.
*
* @param wifiInfo
* @return network speed by one of the WIFI_DRAFT_X type
*/
private float getWifiNetworkSpeed(WifiInfo wifiInfo) {
if (wifiInfo == null) {
return 0;
}
int linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
return linkSpeed;
}