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

3

7

Is there any possible way for calculating mobile and wifi usage of each application in android using TrafficStats' : (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) methods ? I know there must be some way of doing that as 3G watchdog and some other application provide these details.

Can anybody help please ? Thanks

Normand answered 25/11, 2013 at 3:57 Comment(0)
C
6

I've written a similar answer to this type of question here. For getting overall data usage per app it's fairly easy using the methods you named, TrafficStats.getUidTxBytes(int) and TrafficStats.getUidRxBytes(int). However, breaking into mobile and Wifi is not something publicly available currently.

As I mention in my other answer, you'll have to dig into the internal frameworks APIs and try to copy some of the methods used there. You'll definitely need to use NetworkTemplate.buildTemplateWifiWildcard() and NetworkTemplate.buildTemplateMobileWildcard() in order to retrieve the correct NetworkStats for each interface. Once you have the NetworkStats object you'll be able to get your info. Just note: if you're using this in a public app, using internal APIs is very likely to break whenever Google decides to update them.

EDIT: as another tip, you should check out the source for how the Settings app is able to display traffic per app and separate it by Mobile/Wifi. In particular you can look here at their ChartDataLoader, particularly the methods collectHistoryForUid() and loadInBackground()

Cellulosic answered 3/1, 2014 at 5:35 Comment(0)
C
0

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

Christean answered 25/11, 2013 at 4:10 Comment(1)
That method groups usage by interface, but doesn't separate by UID, which is what OP is looking for.Cellulosic
A
0

After a long struggle,I am able to find the Solution for getting data over any interface for each installed Application in android device.

As Android provides TrafficStats Apis but these APIs are providing comple Data stastics for each app uid since device boot and Even APIs are not supporting to get the data over any interface for a particular application. Even if we rely over TraffiucStates APIS ,we get a new data statstics for each Application.

So I thought to use the hidden APIs to USe this..

Here I am mentioning the Steps to get the data statstics for each application over any Interface in Android...

  1. Estabalish a "INetworkStatsSession" session

    import android.net.INetworkStatsSession;
    
    INetworkStatsSession mStatsSession = mStatsService.openSession();
    
  2. Create a Network Templeate according to interafce which you want to measure..

    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()));
    
  3. 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);
    }
    
  4. Collect the network HIStory of respective application byt passing application UIDs...

    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;
    
    }
    
  5. Get the total Consumption data:

    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()));
    
    }
    
Austrasia answered 20/4, 2015 at 11:49 Comment(2)
Where you able to launch it??? Do u have the code in Github for reference?During
Currently subscriber Id is not accessible from Android 10 onwards. Please help to resolve the issueHospitium

© 2022 - 2024 — McMap. All rights reserved.