Can someone explain how TrafficStats works its magic in the Android OS?

后端 未结 2 2001
情书的邮戳
情书的邮戳 2020-12-01 10:16

I\'ve written a nice little Android app to check data usage, unfortunately it relies heavily on android.net.TrafficStats which was introduced with Froyo (Android 2.2).

相关标签:
2条回答
  • 2020-12-01 10:40

    Here's a working, modified version of the code above. This one is using RandomAccessFile and doesn't rely on custom imports but uses only built-in String functions with their Exceptions.

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import android.util.Log;
    
    public class TrafficStatsFile {
    
    private static final String mobileRxFile_1 = "/sys/class/net/rmnet0/statistics/rx_bytes";
    private static final String mobileRxFile_2 = "/sys/class/net/ppp0/statistics/rx_bytes";
    private static final String mobileTxFile_1 = "/sys/class/net/rmnet0/statistics/tx_bytes";
    private static final String mobileTxFile_2 = "/sys/class/net/ppp0/statistics/tx_bytes";
    
    private static final String LOGGING_TAG = TrafficStatsFile.class.getSimpleName();
    
    public long getMobileRxBytes() {
        return tryBoth(mobileRxFile_1, mobileRxFile_2);
    }
    
    public long getMobileTxBytes() {
        return tryBoth(mobileTxFile_1, mobileTxFile_2);
    }
    
    // Return the number from the first file which exists and contains data
    private static long tryBoth(String a, String b) {
        long num = readNumber(a);
        return num >= 0 ? num : readNumber(b);
    }
    
    // Returns an ASCII decimal number read from the specified file, -1 on error.
    private static long readNumber(String filename) {
        try {
            RandomAccessFile f = new RandomAccessFile(filename, "r");
            try {
                Log.d(LOGGING_TAG, "f.length() = " + f.length());
                String contents = f.readLine();
                if(!contents.isEmpty() && contents!=null) {
                    try {
                        return Long.parseLong(contents);
                    }
                    catch(NumberFormatException nfex) {
                        Log.w(LOGGING_TAG, "File contents are not numeric: " + filename); 
                    }
                }
                else {
                    Log.w(LOGGING_TAG, "File contents are empty: " + filename); 
                }
            }
            catch (FileNotFoundException fnfex) {
                Log.w(LOGGING_TAG, "File not found: " + filename, fnfex);
            }
            catch(IOException ioex) {
                Log.w(LOGGING_TAG, "IOException: " + filename, ioex);
            }   
        }catch(FileNotFoundException ffe){
            Log.w(LOGGING_TAG, "File not found: " + filename, ffe);
        }
        return -1;
    }
    
    }
    
    0 讨论(0)
  • 2020-12-01 10:42

    Changing it to a RandomAccessFile instead of File worked.

    Edit: See IBoS's answer for working code. Changing the accepted answer to his.

    0 讨论(0)
提交回复
热议问题