我如何能够将从 TrafficStats.getTotalRxBytes()接收的字节转换为MB,格式为0.5或0.7 MB。
这是我所做的一段话:
TextView txtData = (TextView)findViewById(R.id.txtBytes);
long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
if(rxBytes>=1024){
DecimalFormat formatKB = new DecimalFormat("0");
double rxKb = rxBytes/1024;
txtData.setText("0" + "." + formatKB.format(rxKb) + " KB");
if(rxKb>=1024){
DecimalFormat formatMB = new DecimalFormat("0.0");
double rxMB = rxKb/1024;
txtData.setText(formatMB.format(rxMB) + " MB");
if(rxMB>=1024){
DecimalFormat formatGB = new DecimalFormat("0.0");
double rxGB = rxMB/1024;
txtData.setText(formatGB.format(rxGB) + " GB");
}
}
}
mHandler.postDelayed(mRunnable, 1000);
提前感谢您的帮助! :d