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

前端 未结 2 898
我在风中等你
我在风中等你 2021-02-05 18:49
recived = TrafficStats.getUidRxBytes(uid);
send = TrafficStats.getUidTxBytes(uid);

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

TrafficStats.get         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 19:18

    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.

提交回复
热议问题