Android programmatically get data usage for specific app, for example: Data Usage used on "Facebook"

Wergil picture Wergil · Aug 10, 2015 · Viewed 9.8k times · Source

I would like to categorize the data usage by:

OS ------60% (300MB used so far)
Music ------ 10% (50MB used so far)
Facebook -----5% (25MB used so far)

Anyone can help me in solving this Android programming as I am a beginner of Android development and would like to develop a mobile network monitor app currently. I would like to get the data usage by specifying the app name.

Thank you. Hope to hear from you all.

Answer

rupesh jain picture rupesh jain · Aug 10, 2015

Use http://developer.android.com/reference/android/net/TrafficStats.html#getUidRxBytes(int)

public static long getUidRxBytes (int uid)

Added in API level 8 Return number of bytes received by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also myUid() uid

and

public static long getUidTxBytes (int uid)

Added in API level 8 Return number of bytes transmitted by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also myUid() uid