Android 以编程方式获取特定应用的数据使用情况,例如:"Facebook" 上使用的数据使用情况

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

我想按以下方式对数据使用情况进行分类:

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

任何人都可以帮助我解决这个 Android 编程,因为我是 Android 开发的初学者,目前想开发一个移动网络监控应用程序。 我想通过指定应用程序名称来获取数据使用量。

谢谢。希望收到大家的来信。

使用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

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