Android TrafficStats - Oversized value is a bug?

515 views Asked by At

I am investigating about traffic measuring in android. I am developing on a Galaxy S4 and I programmed a service that catches TrafficStats API one time per minute, saves in SharedPreference the accumulated traffic (AKA BaseTraffic) and saves in database the difference between the current traffic minus BaseTraffic.

The problem is that in short periods (15 min) TrafficStats return an oversized value (1.6 GB per minute) and ever the same value. Someone know if this is a bug or other issue.

excel dump

My code is the next for get the traffic:

public class TrafficTracker {

    public static long getCurrentTraffic() {
        long traff = 0;
        traff = (TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes());
        if (traff > 0) {
            return traff;
        } else {
            throw new UnsupportedOperationException("TrafficStats not supported");
        }

    }

    public static long getTrafficWithOutBase(long baseTraffic) {
        return TrafficStats.getTotalTxBytes() + TrafficStats.getTotalRxBytes() - baseTraffic;
    }
}

And call this code here:

if (preferences.getBaseTraffic() != null) {
    if (TrafficTracker.getCurrentTraffic() > preferences.getBaseTraffic().getByteTraffic()) {

        TrafficObject trafficObject = new TrafficObject(new Date(calendar.getTimeInMillis()), TrafficTracker.getTrafficWithOutBase(preferences.getBaseTraffic().getByteTraffic()));
        daoTraffic.create(trafficObject);

        preferences.setBaseTraffic(new TrafficObject(new Date(System.currentTimeMillis()), preferences.getBaseTraffic().getByteTraffic() + trafficObject.getByteTraffic()));

    } else {//when stats are reseted            
        TrafficObject trafficObject = new TrafficObject(new Date(calendar.getTimeInMillis()), TrafficTracker.getCurrentTraffic());
        daoTraffic.create(trafficObject);
        preferences.setBaseTraffic(trafficObject);          
    }

}

** UPDATE **

I found my error :). I replace >= instead of >. Now works properly when it is disconnected from data or wifi.

    if (TrafficTracker.getCurrentTraffic() >= preferences.getBaseTraffic().getByteTraffic())
1

There are 1 answers

0
ClarkXP On BEST ANSWER

I found my error :). I replace >= instead of >. Now works properly when it is disconnected from data or wifi.

    if (TrafficTracker.getCurrentTraffic() >= preferences.getBaseTraffic().getByteTraffic())