我需要在Android手机上安装的每个应用程序上获取数据。
Ex:我在手机上安装了YouTube应用程序,我需要通过wifi和移动数据来获取YouTube应用的数据。
例外结果:
YouTube - Wifi - 500MB。
YouTube - 移动数据 - 100 KB
我尝试使用TrafficStats API
int mobileTx = TrafficStats.getMobileTxBytes();
int mobileRx = TrafficStats.getMobileRxBytes();
int wifiTx = TrafficStats.getTotalTxBytes() - mobileTx;
int wifiRx = TrafficStats.getTotalRxBytes() - mobileRx;
上面给出了移动整个数据的使用情况,但我需要按每个应用程序获取。
答案 0 :(得分:1)
TrafficStats.getUidRxBytes(int uid)
和TrafficStats.getUidTxBytes(int uid)
可以帮助您解决这种情况,因为这些静态方法提供了自上次启动以来某些UID接收和传输的字节信息。为此,您需要找到您需要详细信息的应用程序的UID。要查找应用程序的UID,您可以查看此主题 - How to get uid value of an android application from a list displayed in a spinner?
答案 1 :(得分:0)
您可以使用TrafficStats类的getUidTxBytes API
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.
Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.
虽然这也表明您必须使用NetworkStatsManager来获取除调用应用之外的网络统计信息历史记录。
此处uid是此过程的标识符