首先,这个问题也在之前被问过(我进行了1小时的研究),但他们似乎都没有回答这个问题。所以这里 - 我希望使用TrafficStats获得当前的上传和下载速度,并在我的textView中显示它,我可以在下面的代码中显示Up和Down统计信息(我从一个问题中获取此代码)
public class MainActivity extends Activity {
private Handler mHandler = new Handler();
private long mStartRX = 0;
private long mStartTX = 0;
private final Runnable mRunnable = new Runnable() {
public void run() {
TextView RX = (TextView) findViewById(R.id.RX);
TextView TX = (TextView) findViewById(R.id.TX);
// long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
// RX.setText(Long.toString(rxBytes));
long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
RX.setText(Long.toString(rxBytes) + " bytes");
if (rxBytes >= 1024) {
// KB or more
long rxKb = rxBytes / 1024;
RX.setText(Long.toString(rxKb) + " KBs");
if (rxKb >= 1024) {
// MB or more
long rxMB = rxKb / 1024;
RX.setText(Long.toString(rxMB) + " MBs");
if (rxMB >= 1024) {
// GB or more
long rxGB = rxMB / 1024;
RX.setText(Long.toString(rxGB));
}// rxMB>1024
}// rxKb > 1024
}// rxBytes>=1024
// long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
// TX.setText(Long.toString(txBytes));
long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
TX.setText(Long.toString(txBytes) + " bytes");
if (txBytes >= 1024) {
// KB or more
long txKb = txBytes / 1024;
TX.setText(Long.toString(txKb) + " KBs");
if (txKb >= 1024) {
// MB or more
long txMB = txKb / 1024;
TX.setText(Long.toString(txMB) + " MBs");
if (txMB >= 1024) {
// GB or more
long txGB = txMB / 1024;
TX.setText(Long.toString(txGB));
}// rxMB>1024
}// rxKb > 1024
}// rxBytes>=1024
mHandler.postDelayed(mRunnable, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
if (mStartRX == TrafficStats.UNSUPPORTED
|| mStartTX == TrafficStats.UNSUPPORTED) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Uh Oh!");
alert.setMessage("Your device does not support traffic stat monitoring.");
alert.show();
} else {
mHandler.postDelayed(mRunnable, 1000);
}
}
}
我认为这个问题可以有两种解决方法,一种方法是每秒找一些重置 trafficStats的方法。另一种方法是通过每秒从前一个值中减去新值来计算它。任何帮助都非常感谢
答案 0 :(得分:0)
您可以在Android中以编程方式找到连接速度,如下所示:
WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
请注意,如果您预计在其他可用网络上使用wifi,则应使用ConnectivityManager来声明首选网络,否则速度可能实际上并未使用
<强>更新强>
请检查以下代码,以帮助您
public static void Traffic(Context context){
if (applications == null) applications=loadAppInfomation(context);
Iterator<AppInfo> iterator=applications.iterator();
double temp_upload=0, temp_download=0;
while (iterator.hasNext()) {
AppInfo temp=iterator.next();
temp_upload=TrafficStats.getUidTxBytes(temp.getUid()) / 1024.0;
temp_download=TrafficStats.getUidRxBytes(temp.getUid()) / 1024.0;
if (temp_upload <= 0) temp_upload=0;
if (temp_download <= 0) temp_download=0;
temp.setUL(temp_upload);
temp.setDL(temp_download);
}
}
答案 1 :(得分:0)
private Handler mHandler = new Handler();
private long mStartRX = 0;
private long mStartTX = 0;
private final Runnable mRunnable = new Runnable() {
public void run() {
long resetdownload=TrafficStats.getTotalRxBytes();
long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
download.setText(Long.toString(rxBytes));
spdtstdownlod.setText(" bytes");
if (rxBytes >= 1024) {
long rxKb = rxBytes / 1024;
download.setText(Long.toString(rxKb));
spdtstdownlod.setText(" KBs");
if (rxKb >= 1024) {
long rxMB = rxKb / 1024;
download.setText(Long.toString(rxMB));
spdtstdownlod.setText(" MBs");
if (rxMB >= 1024) {
long rxGB = rxMB / 1024;
download.setText(Long.toString(rxGB));
spdtstdownlod.setText(" GBs");
}
}
}
mStartRX=resetdownload;
long resetupload=TrafficStats.getTotalTxBytes();
long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
upload.setText(Long.toString(txBytes));
spdtstupload.setText(" bytes");
if (txBytes >= 1024) {
long txKb = txBytes / 1024;
upload.setText(Long.toString(txKb));
spdtstupload.setText(" KBs");
if (txKb >= 1024) {
long txMB = txKb / 1024;
upload.setText(Long.toString(txMB));
spdtstupload.setText(" MBs");
if (txMB >= 1024) {
long txGB = txMB / 1024;
upload.setText(Long.toString(txGB));
spdtstupload.setText(" GBs");
}
}
}
mStartTX=resetupload;
mHandler.postDelayed(mRunnable, 1000);
}
};