13
recived = TrafficStats.getUidRxBytes(uid);
send = TrafficStats.getUidTxBytes(uid);

TrafficStats.getMobileRxBytes(); 
TrafficStats.getMobileTxBytes(); 

TrafficStats.getTotalRxBytes();
TrafficStats.getTotalTxBytes();

i have application id and i can get that application data usage but i cant get information in more detailed like data usage using wifi and network. i want to find both separately for all installed application in device. how can i find that.? i have already tried many things but didn't get any solution for that.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99

2 Answers2

5

I think you should use alternative way, get traffic by UID, for example — getUidRxPackets(int uid)

Also helpful (how get UID): (How to get all apps installed on android phone)

EDIT:

can you tell me how i can determine how much data use by wifi and mobile.??

There a nice answer — android statistic 3g traffic for each APP, how? look his logic.

Community
  • 1
  • 1
artemiygrn
  • 1,036
  • 7
  • 18
  • i know that very well. but as i already said, many application made like this thats why i am asking of that. – Sagar Maiyad Dec 06 '13 at 10:27
  • i alreadyed tried all that brother. can you tell me how i can determine how much data use by wifi and mobile.?? – Sagar Maiyad Dec 06 '13 at 11:21
  • please, first understand my quetion brother. you still not understand. – Sagar Maiyad Dec 06 '13 at 11:33
  • Why, i think this is solution for you answer above: http://stackoverflow.com/a/12837108/1990485, bottom of the message he explain his logic. – artemiygrn Dec 06 '13 at 11:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42641/discussion-between-kvirair-and-segi) – artemiygrn Dec 06 '13 at 11:36
  • i already know the logic of that. ok, just give me one example in which i can determine data usage by particular application using wifi and mobile both.. – Sagar Maiyad Dec 06 '13 at 11:37
2

Unfortunately there are no public APIs to get that information as of API level 19; you've listed the TrafficStats APIs that are publicly available. Android does it using internal APIs, so unless you're looking to go down that road (which is a really BAD idea and HIGHLY discouraged by Google), you're out of luck for now.

EDIT:
If you INSIST on using the internal APIs and risk your code breaking in various API versions, refer to the following links and be sure to read them thoroughly:
Recompiling Android SDK to use hidden and internal APIs
Recompiling Android SDK for Gingerbread and later

Once you have this implemented, this file will give you the most help in figuring it out:

packages/apps/Settings/src/com/android/settings/fuelgauge/BatteryStatsHelper.java

You'll have to either copy a lot of the code from the file or look for the processAppUsage() function and pull whatever you need. In particular, look for these lines:

        // Add cost of wifi traffic
        final long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, mStatsType);
        final long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, mStatsType);

One final note: Android JUST changed this code in KitKat (API 19) and is likely to change this again in the future. I cannot stress enough how this will break your app once they decide to change again.

anddev84
  • 1,473
  • 12
  • 30
  • then how data manager and network monitor applications works on that?? – Sagar Maiyad Dec 06 '13 at 08:13
  • Internally, there are methods in Android that are capable of doing the breakdown of data usage per UID, and determine what is mobile data vs what is wifi data. However Google has thus far chosen not to make those public for 3rd party developers to use. If you are looking to use the internal APIs in order to gain access, as I mentioned, it would work for now, but is liable to break whenever Google changes their code next (like they did from 4.3 -> 4.4). – anddev84 Dec 06 '13 at 13:29
  • Also, other methods that are posted here about tracking Connectivity changes for when Wifi is connected and then doing the math yourself will get you fairly close, but you are not guaranteed for ALL data usage to be over Wifi just because Wifi is connected. So anyone who is not doing this and is claiming to be able to get these values separately is using the internal APIs. – anddev84 Dec 06 '13 at 17:07
  • can you tell me how can i use internal APIs? – Sagar Maiyad Dec 07 '13 at 04:21