I've written a similar answer to this type of question here. For getting overall data usage per app it's fairly easy using the methods you named, TrafficStats.getUidTxBytes(int)
and TrafficStats.getUidRxBytes(int)
. However, breaking into mobile and Wifi is not something publicly available currently.
As I mention in my other answer, you'll have to dig into the internal frameworks APIs and try to copy some of the methods used there. You'll definitely need to use NetworkTemplate.buildTemplateWifiWildcard()
and NetworkTemplate.buildTemplateMobileWildcard()
in order to retrieve the correct NetworkStats
for each interface. Once you have the NetworkStats
object you'll be able to get your info. Just note: if you're using this in a public app, using internal APIs is very likely to break whenever Google decides to update them.
EDIT: as another tip, you should check out the source for how the Settings app is able to display traffic per app and separate it by Mobile/Wifi. In particular you can look here at their ChartDataLoader
, particularly the methods collectHistoryForUid()
and loadInBackground()