来自特定应用程序的流量统计

jla*_*nza 5 android network-programming

我正在尝试获取我的应用程序生成的流量。它包括视频流,我想获取网络使用数据。

我使用了 TrafficStats 的 Mobile 和 Total 功能。现在我想使用应用程序 UID 将其设置为工作,但我还没有成功,因为它总是返回 -1。我使用的是 Android 2.3.4,代码如下:

int uid = getApplication().getApplicationInfo().uid;
uid = android.os.Process.myUid();
long txApp = TrafficStats.getUidTxBytes(uid);
long rxApp = TrafficStats.getUidRxBytes(uid);
Run Code Online (Sandbox Code Playgroud)

对于这两个 uid,我在 TrafficStats 上收到错误 (-1)。有什么帮助吗?

Pau*_*sma 4

您的设备似乎不支持TrafficStats. 文档指出:

如果该设备不支持统计,将返回UNSUPPORTED。

的代码TrafficStats.UNSUPPORTED-1.

您可以改为阅读/sys/class/net/日志文件。尝试此答案中提供的代码。