2013-08-07 78 views
1

下面是在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代碼方法

在此先感謝

回答

17

通過傳遞字節來調用方法。將執行計算後返回的字符串添加到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]; 
    } 
+0

謝謝,我明白了。 – user45678

+0

@ user574639你是什麼意思「櫃檯運行速度非常快」?在您的應用程序中運行的速度與Chiranjib的方法無關,這是正確的。 – Simon

+0

@Simon - 不是正確的回答方式。 – user45678

0

簡單地傳遞bytes你從getTotal**Bytes()humanReadableByteCount方法。並根據精度(如1KB = 1000字節或1024字節)發送si爲真或假。

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); 
+0

你能真正顯示爲代碼?我正在編輯我的代碼。 – user45678

+0

感謝user2550754。我明白了......愚蠢的變化是它 – user45678

+0

如果它的工作,那麼你應該接受答案,並upvote它。這將有助於未來的訪問者找到答案。 – surhidamatya

0

只需使用一些數學公式如下圖所示,你需要:

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

0

爲KB:((float) Math.round((sizeInBytes/(1024)) * 10)/10)

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

爲GB:((float) Math.round((sizeInBytes/(1024 * 1024 * 1024)) * 10)/10)

相關問題