如何通过wifi和移动数据在android上获得数据使用率?

如何通过wifi和移动数据在android上获得数据使用率?,android,android-wifi,android-data-usage,mobile-data,Android,Android Wifi,Android Data Usage,Mobile Data,我需要获得安卓手机上安装的每个应用程序的数据使用情况 我在手机上安装了YouTube应用程序,我需要通过wifi和手机数据获取YouTube应用程序的数据使用情况 例外结果: YouTube-Wifi-500MB YouTube-移动数据-100KB 我尝试使用TrafficStats API int mobileTx = TrafficStats.getMobileTxBytes(); int mobileRx = TrafficStats.getMobileRxBytes(); int wi

我需要获得安卓手机上安装的每个应用程序的数据使用情况

我在手机上安装了YouTube应用程序,我需要通过wifi和手机数据获取YouTube应用程序的数据使用情况

例外结果:

YouTube-Wifi-500MB

YouTube-移动数据-100KB

我尝试使用TrafficStats API

int mobileTx = TrafficStats.getMobileTxBytes();
int mobileRx = TrafficStats.getMobileRxBytes();
int wifiTx = TrafficStats.getTotalTxBytes() - mobileTx;
int wifiRx = TrafficStats.getTotalRxBytes() - mobileRx;
上面给出了移动设备的全部数据使用情况,但我需要获得每个应用程序的数据。

您可以使用TrafficStats类中的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.
虽然这也告诉你,你将不得不使用来获取除呼叫应用程序以外的其他应用程序的网络统计历史记录


在这里,uid是这个进程的uid的标识符。getUidRxBytes(int uid)和
TrafficStats。getUidTxBytes(int uid)
可以帮助您解决这种情况,因为这些静态方法提供一些uid自上次启动以来接收和传输的字节的信息。为此,您需要找到需要详细信息的应用程序的UID。要查找应用程序的UID,您可以查看此线程-

如何显示数据使用情况,如日、周和月?系统不会按月、周或日跟踪它。如果您想要这样的功能,您可以跟踪应用程序安装后使用的数据并定期计算。谢谢@deepak。我得到了每个应用程序的数据使用情况。如何单独拆分移动和wifi数据使用情况(每个应用程序)。您好,我需要从我的应用程序中检查这些内容(每个应用程序在特定时间段/特定天数内的数据使用情况)。