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

后端 未结 3 1994
南笙
南笙 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:33

    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()));
      
      }
      

提交回复
热议问题