(@Pabloku, sorry this answer is coming so late, hopefully it will still be of some help)
Firstly, if you're looking to get traffic stats for individual apps, but only on 3G, it's not possible using public APIs. Android provides the TrafficStats.getUidTxBytes(int)
and TrafficStats.getUidRxBytes(int)
as public APIs to get the total number of bytes used by apps, but nothing (public) to separate them by interface.
Assuming this doesn't ruin your day, here is a pseudocode algorithm for how to do the rest of what you mention:
- Set an alarm if necessary (using
AlarmManager
) for the start of your range, and store these values (presumably in a DB). Reason: you may need to subtract these existing TrafficStats
values as an offset if they are > 0 at the time your date range starts.
- Also set an alarm for the end of your range.
- Create a
BroadcastReceiver
to receive ACTION_SHUTDOWN.
- In your
BroacastReceiver
, note down the TrafficStats
for your app(s) at shutdown. Reason: TrafficStats
will get reset on every reboot.
- If this is the first shutdown since start: subtract your initial offset and store that final value (being careful to remove the initial offset)
- Otherwise, whatever value is reported will be accurate since boot.
- Once your end alarm is triggered, note down the
TrafficStats
at that point, and add all previously collected stats
- (if somehow the phone never rebooted between start and end, just do endStats - startStats).
Good luck!