How to find wifi and network data usage separately

2019-03-15 05:48发布

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.

2条回答
淡お忘
2楼-- · 2019-03-15 06:15

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.

查看更多
Anthone
3楼-- · 2019-03-15 06:28

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.

查看更多
登录 后发表回答