查找其他 Android 应用产生了多少网络流量

发布于 2024-10-23 19:37:48 字数 1670 浏览 9 评论 0原文

我正在尝试制作一个后台服务,该服务应该测量各种应用程序的流量使用情况,以便能够向用户显示哪些应用程序消耗最多的数据流量。

我发现 Spare Parts 应用程序正是这样做的,但在 1.6 Dell Streak 设备上安装它后,我总是在“网络使用情况”中得到“没有可用的电池使用数据”。这个功能在备件中是否有效? 另外,我找不到可用的备件源代码。

https://android.googlesource.com/platform/development/ +/froyo-release/apps/SpareParts
看起来已经过时或不完整。 (?)

但是 Spare Parts 似乎可以测量每个应用程序的 CPU 使用情况。它如何在未root的手机上做到这一点?

我对如何测量每个应用程序的流量的一般想法是定期检查

"sys/class/net/" + sWiFiInterface + "/statistics/rx_bytes"

"sys/class/net/ " + sWiFiInterface + "/statistics/tx_bytes"

"sys/class/net/" + sMobileInterface + "/statistics/rx_bytes"

"sys/class/net/" + sMobileInterface +“/statistics/tx_bytes”

文件,并查看哪个应用程序当前具有焦点,从而最有可能导致生成的网络流量。

不幸的是,我找不到如何让应用程序当前获得焦点。

我发现了这个:

Android,如何获取有关当前显示的活动的信息(在前面)?

但似乎这是关于测试,而不仅仅是在非 root Android 设备上运行的 3d 方服务。

我们可以使用 ActivityManager.getCurrentTasks() 获取正在运行的活动,但其中任何活动都可以成为焦点。出于安全考虑,Android 架构师似乎明确不希望 3d 方应用程序知道哪个应用程序具有焦点

(请参阅 http://android.bigresource.com/Track/android-zb2mhvZX4/)。

有办法解决这个问题吗?

另外,如果我不仅想检测哪些活动消耗了流量,还想检测哪些服务,我可以使用以下命令获取所有当前正在运行的服务 ActivityManager.getCurrentServives() 甚至查看每一个是否处于前台模式(不像 Android 需要资源时被抛出)。但这并没有给我带来任何帮助。

有什么想法吗?

I am trying to make a background service which should measure traffic usage of various applications so as to be able to show to the user which apps consume most data traffic.

I found that Spare Parts app does exactly that, but after installing it on a 1.6 Dell Streak device I always get "No battery usage data available" for "Network usage". Does this function at all work in Spare Parts?
Also, I couldn't find a working source code for Spare Parts.

https://android.googlesource.com/platform/development/+/froyo-release/apps/SpareParts
looks to be outdated or incomplete. (?)

But Spare Parts seems to measure e.g. CPU usage per app. How does it do that on an unrooted phone?

My general idea of how traffic per app could be measured is to regularly check the

"sys/class/net/" + sWiFiInterface + "/statistics/rx_bytes"

"sys/class/net/" + sWiFiInterface + "/statistics/tx_bytes"

"sys/class/net/" + sMobileInterface + "/statistics/rx_bytes"

"sys/class/net/" + sMobileInterface + "/statistics/tx_bytes"

files and to see which app currently has focus and thus most likely to cause the generated network traffic.

Unfortunately I can't find how to get the app currently having focus.

I found this:

Android, how to get information on which activity is currently showing (in foregorund)?

but seems it's about testing, not just a 3d party service running on non-rooted Android device.

We can get what activities are running with ActivityManager.getCurrentTasks(), but any of them can be the one with focus. It seems like the Android architects explicitly don't want 3d party apps to know what app has focus, because of security concerns

(see http://android.bigresource.com/Track/android-zb2mhvZX4/).

Is there a way around this?

Also, if I want to not only detect which activities eat up traffic but also what services, I can get all currently running services with
ActivityManager.getCurrentSerives()
and even see for each one if it's in foreground mode (unlike to be thrown out if Android needs resources). But this again doesn't bring me any far.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2024-10-30 19:37:48

您可以使用 ActivityManager.getRunningAppProcesses 调用检测当前的前台应用程序。它将返回 RunningAppProcessInfo 记录列表。要确定哪个应用程序位于前台,请检查 RunningAppProcessInfo.importance 字段是否等于 RunningAppProcessInfo.IMPORTANCE_FOREGROUND

但请注意,对 ActivityManager.getRunningAppProcesses() 方法的调用必须不在 UI 线程中执行。只需在后台线程中调用它(例如通过 AsyncTask),它就会返回正确的结果。检查我的帖子< /a> 了解更多详细信息。

You can detect currently foreground application with ActivityManager.getRunningAppProcesses call. It will return a list of RunningAppProcessInfo records. To determine which application is on foreground check RunningAppProcessInfo.importance field for equality to RunningAppProcessInfo.IMPORTANCE_FOREGROUND.

But be ware that call to ActivityManager.getRunningAppProcesses() method must be performed NOT in the UI thread. Just call it in the background thread (for example via AsyncTask) and it will return correct results. Check my post for additional details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文