Calculation of the use of mobile and wifi data of each application in android

Is there any possible way to calculate the usage of mobile and Wi-Fi of each application in android using the TrafficStats methods: (getUidRxBytes, getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes, getMobileTxBytes)? I know there must be some way to do this, since the 3G watchdog and another application provide this data.

Can anyone help? Thanks

+4
source share
3 answers

. , , TrafficStats.getUidTxBytes(int) TrafficStats.getUidRxBytes(int). , Wi-Fi .

, API . NetworkTemplate.buildTemplateWifiWildcard() NetworkTemplate.buildTemplateMobileWildcard(), NetworkStats . NetworkStats, . :, , API , Google .

EDIT: , Settings /Wi -Fi. , ChartDataLoader, collectHistoryForUid() loadInBackground()

+5

android .

Android TrafficStats Apis, API API . TraffiucStates APIS, statstics .

API-, .

, statstics Android...

  • "INetworkStatsSession"

    import android.net.INetworkStatsSession;
    
    INetworkStatsSession mStatsSession = mStatsService.openSession();
    
  • interafce, .

    import static android.net.NetworkTemplate.buildTemplateEthernet;
    import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
    import static android.net.NetworkTemplate.buildTemplateMobile4g;
    import static android.net.NetworkTemplate.buildTemplateMobileAll;
    import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
    
    import android.net.NetworkTemplate;
    
    private NetworkTemplate mTemplate;
    
    mTemplate = buildTemplateMobileAll(getActiveSubscriberId(this
                .getApplicationContext()));
    
  • GetActive SubcriberID:

    private static String getActiveSubscriberId(Context context) {
        final TelephonyManager tele = TelephonyManager.from(context);
        final String actualSubscriberId = tele.getSubscriberId();
        return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
    }
    
  • HIStory byt, UID ...

    private NetworkStatsHistory collectHistoryForUid(NetworkTemplate template,
            int uid, int set) throws RemoteException {
        final NetworkStatsHistory history = mStatsSession.getHistoryForUid(
                template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
        return history;
    
    }
    
  • :

    public void showConsuption(int UID){
        NetworkStatsHistory history = collectHistoryForUid(mTemplate, UID, SET_DEFAULT);
        Log.i(DEBUG_TAG, "load:::::SET_DEFAULT:.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
        history = collectHistoryForUid(mTemplate, 10093, SET_FOREGROUND);
        Log.i(DEBUG_TAG, "load::::SET_FOREGROUND::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
        history = collectHistoryForUid(mTemplate, 10093, SET_ALL);
        Log.i(DEBUG_TAG, "load::::SET_ALL::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
    
    }
    
-1

Android 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 

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

, . , , .

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

-2
source

All Articles