如何计算android中每个应用程序的移动和wifi数据使用情况?

use*_*302 7 android usage-statistics android-data-usage

有没有办法使用TrafficStats'(getUidRxBytes,getUidTxBytes,getTotalRxbytes,getTotalTXbytes,getMobileRxBytes,getMobileTxBytes)方法计算Android中每个应用程序的移动和wifi使用情况?我知道必须有一些方法可以做到这一点,因为3G看门狗和其他一些应用程序提供了这些细节.

有人可以帮忙吗?谢谢

and*_*v84 6

我写了一个类似的答案,这样的问题在这里.为了得到每个应用程序总体数据的使用是相当容易使用,你命名的方法,TrafficStats.getUidTxBytes(int)TrafficStats.getUidRxBytes(int).然而,打入移动和Wifi并不是目前公开的东西.

正如我在其他答案中提到的那样,您将不得不深入研究内部框架API并尝试复制其中使用的一些方法.你肯定需要使用NetworkTemplate.buildTemplateWifiWildcard()和为每个接口NetworkTemplate.buildTemplateMobileWildcard()检索正确NetworkStats的.获得该NetworkStats对象后,您将能够获得您的信息.请注意:如果您在公共应用中使用此功能,那么每当Google决定更新它们时,使用内部API 可能会中断.

编辑:作为另一个提示,您应该查看设置应用程序如何能够显示每个应用程序的流量并通过移动/ Wifi分隔它的来源.特别是你可以看看他们ChartDataLoader,特别是方法collectHistoryForUid()loadInBackground()


Rob*_*bin 0

Android框架中有一个隐藏类,称为NetworkStats:

/**
 * Collection of active network statistics. Can contain summary details across
 * all interfaces, or details with per-UID granularity. Internally stores data
 * as a large table, closely matching {@code /proc/} data format. This structure
 * optimizes for rapid in-memory comparison, but consider using
 * {@link NetworkStatsHistory} when persisting.
 *
 * @hide
 */
public class NetworkStats implements Parcelable 
Run Code Online (Sandbox Code Playgroud)

它有一个方法叫做

/**
 * Return total statistics grouped by {@link #iface}; doesn't mutate the
 * original structure.
 */
public NetworkStats groupedByIface()
Run Code Online (Sandbox Code Playgroud)

我想这个方法可以满足你的要求。您可能需要参考源代码并查看是否可以为您自己的目的提取有用的信息。

https://android.googlesource.com/platform/frameworks/base/+/android-4.3_r2.3/core/java/android/net/NetworkStats.java