将接收的字节转换为KB/MB-Android

将接收的字节转换为KB/MB-Android,android,Android,我使用以下代码显示发送和接收的字节: 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

我使用以下代码显示发送和接收的字节:

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 txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            TX.setText(Long.toString(txBytes));
            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);  
        }
    }
}

但我想增加单位,比如KB,MB,GB。例如,如果数据使用量小于1MB,则应以KB为单位显示,并应分别更改为MB和GB。

此代码适用于RX:

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
它假设totalRX是字节,并用单位设置文本 然后深入,如果大小满足KB、MB、GB,则设置文本 如果对您有效,请使用相同的TX代码

注意:如果您不喜欢multi-setText调用 您可以声明临时变量(大小和单位),在深入if语句时填充它们,然后在主if语句下面设置一次

用这个

 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];
     }
}

我在这里问了另一个问题我喜欢你的溶胶,纯数学,这是我的弱点:,投赞成票
 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];
     }
}