How to find wifi and network data usage separately by particular application in android?

雨燕双飞 提交于 2019-12-03 03:14:14
artemiygrn

I think you should use alternative way, get traffic by UID, for example — getUidRxPackets(int uid)

Also helpful (how get UID): (How to get all apps installed on android phone)

EDIT:

can you tell me how i can determine how much data use by wifi and mobile.??

There a nice answer — android statistic 3g traffic for each APP, how? look his logic.

Unfortunately there are no public APIs to get that information as of API level 19; you've listed the TrafficStats APIs that are publicly available. Android does it using internal APIs, so unless you're looking to go down that road (which is a really BAD idea and HIGHLY discouraged by Google), you're out of luck for now.

EDIT:
If you INSIST on using the internal APIs and risk your code breaking in various API versions, refer to the following links and be sure to read them thoroughly:
Recompiling Android SDK to use hidden and internal APIs
Recompiling Android SDK for Gingerbread and later

Once you have this implemented, this file will give you the most help in figuring it out:

packages/apps/Settings/src/com/android/settings/fuelgauge/BatteryStatsHelper.java

You'll have to either copy a lot of the code from the file or look for the processAppUsage() function and pull whatever you need. In particular, look for these lines:

        // Add cost of wifi traffic
        final long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, mStatsType);
        final long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, mStatsType);

One final note: Android JUST changed this code in KitKat (API 19) and is likely to change this again in the future. I cannot stress enough how this will break your app once they decide to change again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!