Find how much network traffic other Android apps generate
Asked Answered
M

1

4

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?

Mccain answered 16/3, 2011 at 15:56 Comment(0)
L
3

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.

Leonerd answered 16/3, 2011 at 22:26 Comment(1)
Thanks a lot for your reply! I guess this solves a big part of my problem. I just found NetSentry app (source code) which even does traffic measurement without a background service running. Now trying to figure out if i could do it the same way and also try to attribute traffic to activity at foreground.Mccain

© 2022 - 2024 — McMap. All rights reserved.