Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate mobile and wifi data usage of each application in android?

Is there any possible way for calculating mobile and wifi usage of each application in android using TrafficStats' : (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) methods ? I know there must be some way of doing that as 3G watchdog and some other application provide these details.

Can anybody help please ? Thanks

like image 384
user2011302 Avatar asked Nov 25 '13 03:11

user2011302


People also ask

How can I track WIFI usage on Android?

Open Settings | Data Usage. From that window, tap the menu button (three vertical dots in the upper right corner) and tap Show Wi-Fi (Figure D). Once it's enabled, you'll see a Wi-Fi tab listed (Figure E)–tap on that tab, and you can view your Wi-Fi data usage in the same way as you can cellular usage.

How can I track my mobile WIFI usage?

Go to the Account tab. At the top of the screen, you'll see your current data usage. Android: To see your daily breakdown select View overall or Daily data usage.


1 Answers

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()

like image 158
anddev84 Avatar answered Sep 29 '22 11:09

anddev84