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

妖精的绣舞 提交于 2019-12-20 12:08:38

问题


recived = TrafficStats.getUidRxBytes(uid);
send = TrafficStats.getUidTxBytes(uid);

TrafficStats.getMobileRxBytes(); 
TrafficStats.getMobileTxBytes(); 

TrafficStats.getTotalRxBytes();
TrafficStats.getTotalTxBytes();

i have application id and i can get that application data usage but i cant get information in more detailed like data usage using wifi and network. i want to find both separately for all installed application in device. how can i find that.? i have already tried many things but didn't get any solution for that.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/20028124/how-to-find-wifi-and-network-data-usage-separately-by-particular-application-in

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