下面是在String中返回數據計數器值的類。我想字符串格式爲KB,MB和GB將字符串格式化爲KB,MB和GB
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView infoView = (TextView)findViewById(R.id.traffic_info);
String info = "";
long getmobilerxbytes = TrafficStats.getMobileRxBytes();
long getmobilerxpackets = TrafficStats.getMobileRxPackets();
long getmobiletxbytes = TrafficStats.getMobileTxBytes();
long getmobiletxpackets = TrafficStats.getMobileTxPackets();
long totalrxbytes = TrafficStats.getTotalRxBytes();
long totalrxpackets = TrafficStats.getTotalRxPackets();
long totaltxbytes = TrafficStats.getTotalTxBytes();
long totaltxpackets = TrafficStats.getTotalTxPackets();
info += "Mobile Interface:\n";
info += ("\tReceived: " + getmobilerxbytes + " bytes/" + getmobilerxpackets + " packets\n");
info += ("\tTransmitted: " + getmobiletxbytes + " bytes/" + getmobiletxpackets + " packets\n");
info += "All Network Interface:\n";
info += ("\tReceived: " + totalrxbytes + " bytes/" + totalrxpackets + " packets\n");
info += ("\tTransmitted: " + totaltxbytes + " bytes/" + totaltxpackets + " packets\n");
infoView.setText(info);
}
這裏是美麗的方法,這樣做:
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes)/Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes/Math.pow(unit, exp), pre);
}
但我不知道我怎麼可以在上面使用在我的onCreate代碼方法
在此先感謝
謝謝,我明白了。 – user45678
@ user574639你是什麼意思「櫃檯運行速度非常快」?在您的應用程序中運行的速度與Chiranjib的方法無關,這是正確的。 – Simon
@Simon - 不是正確的回答方式。 – user45678