How to calculate mobile and wifi data usage of each application in android?

后端 未结 3 2002
南笙
南笙 2021-01-02 15:00

Is there any possible way for calculating mobile and wifi usage of each application in android using TrafficStats\' : (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTota

3条回答
  •  攒了一身酷
    2021-01-02 15:53

    There is a hidden class in Android framework called NetworkStats:

    /**
     * Collection of active network statistics. Can contain summary details across
     * all interfaces, or details with per-UID granularity. Internally stores data
     * as a large table, closely matching {@code /proc/} data format. This structure
     * optimizes for rapid in-memory comparison, but consider using
     * {@link NetworkStatsHistory} when persisting.
     *
     * @hide
     */
    public class NetworkStats implements Parcelable 
    

    And it has a method called

    /**
     * Return total statistics grouped by {@link #iface}; doesn't mutate the
     * original structure.
     */
    public NetworkStats groupedByIface()
    

    And I suppose this method will fulfill your requirement. You might need to refer to the source code and to see if you can extract the useful information for your own purpose.

    https://android.googlesource.com/platform/frameworks/base/+/android-4.3_r2.3/core/java/android/net/NetworkStats.java

提交回复
热议问题