5

下面是返回字符串中的数据计数器值的类。我想将字符串格式化为 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 代码中使用上述方法

提前致谢

4

4 回答 4

39

通过传递字节来调用该方法。将计算后返回的字符串添加到 TextView。

public static String getFileSize(long size) {
    if (size <= 0)
        return "0";

    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));

    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
于 2013-08-07T09:40:39.193 回答
1

只需在需要的地方使用下面显示的一些数学公式:

if(bytes < 1024)
    return bytes + " bytes";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " KB";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " MB";

bytes /= 1024;
if(bytes < 1024)
    return bytes + " GB";

return String(bytes);

并将其传递给函数

humanReadableByteCount(bytes,true);

其中字节等于 TrafficStats.getTotalTxBytes()

希望能帮助到你。我在堆栈上也发现了一些链接link1link2

于 2013-08-07T09:41:09.143 回答
0

只需将bytes您从getTotal**Bytes()tohumanReadableByteCount方法传递给您。si并根据精度发送为真或假(如您想要 1KB=1000 字节或 1024 字节)。

 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 = "";

    info += "Mobile Interface:\n";
    info += ("\tReceived: " + TrafficStats.getMobileRxBytes() + " bytes / " + TrafficStats.getMobileRxPackets() + " packets\n");
    info += ("\tTransmitted: " + TrafficStats.getMobileTxBytes() + " bytes / " + TrafficStats.getMobileTxPackets() + " packets\n");

    info += "All Network Interface:\n";
    info += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes / " + TrafficStats.getTotalRxPackets() + " packets\n");

    info += humanReadableByteCount(TrafficStats.getTotalRxBytes(),true);

    info += ("\tTransmitted: " + TrafficStats.getTotalTxBytes() + " bytes / " + TrafficStats.getTotalTxPackets() + " packets\n");

    info += humanReadableByteCount(TrafficStats.getTotalTxBytes(),true);

    infoView.setText(info);
于 2013-08-07T09:35:14.897 回答
0

对于知识库: ((float) Math.round((sizeInBytes / (1024)) * 10) / 10)

对于 MB: ((float) Math.round((sizeInBytes / (1024 * 1024)) * 10) / 10)

国标: ((float) Math.round((sizeInBytes / (1024 * 1024 * 1024)) * 10) / 10)

于 2013-08-07T09:42:30.517 回答