2010-10-27 2 views
6

Ich habe eine nette kleine Android-App geschrieben, um die Datennutzung zu überprüfen, leider hängt es stark von android.net.TrafficStats, die mit Froyo (Android 2.2) eingeführt wurde.Kann jemand erklären, wie TrafficStats im Android OS funktioniert?

Ich versuche, für meine Nicht-Froyo Benutzer diese Klasse-Port zurück, und was ich bin in der Lage von der Android source zu bestimmen ist:

  1. TrafficStats.java ist nur ein gebürtiger Zeiger auf ac Datei
  2. Die c-Datei zwei Dateien öffnet (siehe unten) und liest dessen Inhalt
  3. Wenn entweder einen numerischen Wert enthält es spuckt als „Bytes verwendet“ zurück mein challeng

Hier zählen e ... wenn ich TrafficStats über die API auf meinem Gerät anrufe, bekomme ich eine Lesung (z. B. 1113853 Bytes). Wenn ich die zwei Dateien öffne und ihren Inhalt überprüfe, existiert eine Datei nicht und die andere Datei ist 0 Bytes.

So klar habe ich falsch verstanden, was TrafficStats macht. Kann jemand etwas Licht darüber werfen, wie es funktioniert, ist es Magie?

Danke für die Hilfe.

(hier ist mein Versuch, in dem Hafen der c-Datei java)

package com.suttco.net; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import com.suttco.IOUtils; 
import com.suttco.StringUtils; 

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) { 
    File f = new File(filename); 
    if(f.exists()) { 
    if(f.canRead()) { 
    try { 
    Log.d(LOGGING_TAG, "f.length() = " + f.length()); 
    String contents = IOUtils.readFileAsString(f); 
    if(StringUtils.IsNotNullOrEmpty(contents)) { 
     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); 
    } 
    } 
    else { 
    Log.w(LOGGING_TAG, "Unable to read file: " + filename); 
    } 
    } 
    else { 
    Log.w(LOGGING_TAG, "File does not exist: " + filename); 
    } 
    return -1; 
} 
} 
+0

Link zur Quelle bekam verstümmelten, versuchen Sie dies: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/jni/android_net_TrafficStats .cpp; h = ff46bdd2ba39380a0a673cbf8aadfc5a3529156c; hb = refs/heads/master –

+0

Oder kopieren Sie einen Cache hier: http://google.com/codesearch#uX1GffpyOZk/core/jni/android_net_TrafficStats.cpp&q=TrafficStats.cpp – Idolon

+0

Der Link ist beschädigt. Und selbst wenn Sie die android_net_TrafficStats.cpp finden, wurde der Code, der sich mit diesen Zählern beschäftigt, von dort auf die neueste Version verschoben. Also, der Link zur richtigen Revision ist hier: https://github.com/android/platform_frameworks_base/blob/3762c311729fe9f3af085c14c5c1fb471d994c03/core/jni/android_net_TrafficStats.cpp – Eduardo

Antwort

11

Hier ist eine funktionierende, modifizierte Version des obigen Codes. Dieser verwendet RandomAccessFile und verlässt sich nicht auf benutzerdefinierte imports, sondern verwendet nur eingebaute String Funktionen mit ihrer 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; 
} 

} 
+1

danke, dass Sie mir -1 für nichts geben ... – IBoS

+0

Keine der Folgende Verzeichnisse sind in meinem Gerät vorhanden: rmnet0, ppp0, rmnet0 und ppp0. Ich benutze Samsung SM-T111. Ist das Gerät abhängig? – Gem

4

es zu einem Random Ändern statt Datei gearbeitet.

Bearbeiten: Siehe IBoS-Antwort für Arbeitscode. Ändern der akzeptierten Antwort auf seine.

Verwandte Themen