android statistic 3g traffic for each APP, how?
Asked Answered
G

4

15

For statistic network traffic per APP, what I'm using now is Android TrafficStats

That I can get result like following :

  • Youtube 50.30 MBytes
  • Facebook 21.39 MBytes
  • Google Play 103.38 MBytes
  • (and more...)

As I know, the "Android Trafficstats" just a native pointer to a c file. (maybe an .so ?)

But it mixed Wifi & 3g traffic, is there any way to only get non-WiFi traffic statistic ?

Garnish answered 27/9, 2012 at 2:53 Comment(1)
What system are you using? from at least ICS forward you can see data usage by 3g/mobile ; wlan ; or all combined.Willy
G
8

Evening all, I got some way to do that...

First I have to create a class which extends BroadcasrReceiver, like this:

Manifest definition:

<receiver android:name=".core.CoreReceiver" android:enabled="true" android:exported="false">
  <intent-filter>
    <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
    <action android:name="android.net.wifi.STATE_CHANGE" />
  </intent-filter>
</receiver>

Codes:

/**
 * @author me
 */
public class CoreReceiver extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
    if (Constants.phone == null) {
      // Receive [network] event
      Constants.phone=new PhoneListen(context);
      TelephonyManager telephony=(TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);
      telephony.listen(Constants.phone, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    }

    WifiManager wifi=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    boolean b=wifi.isWifiEnabled();
    if (Constants.STATUS_WIFI != b) {
       // WiFi status changed...
    }
  }
}

And a phone stats listener below...

public class PhoneListen extends PhoneStateListener {
  private Context context;    
  public PhoneListen(Context c) {
     context=c;
  }    
  @Override
  public void onDataConnectionStateChanged(int state) {
    switch(state) {
      case TelephonyManager.DATA_DISCONNECTED:// 3G
        //3G has been turned OFF
      break;
      case TelephonyManager.DATA_CONNECTING:// 3G
        //3G is connecting
      break;
      case TelephonyManager.DATA_CONNECTED:// 3G
        //3G has turned ON
      break;
    }
  }
}

Finally, here's my logic

  1. Collect count into SQLite DB.
  2. Collect all app network usage via TrafficStats every 1 minute, only when 3G is ON.
  3. If 3G is OFF, then stop collecting.
  4. If both 3G & WiFi are ON, stop collecting.

As I know, network traffic will go through WiFi only, if both 3G & WiFi are available.

Garnish answered 11/10, 2012 at 10:7 Comment(0)
T
5

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 compile Data statistics 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 statistics for each Application.

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

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

  1. Establish a "INetworkStatsSession" session

    import android.net.INetworkStatsSession;
    INetworkStatsSession mStatsSession = mStatsService.openSession();
    
  2. Create a Network Template according to interface 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 SubscriberID:

    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()));
    
    }
    
Thorny answered 20/4, 2015 at 11:57 Comment(2)
Do you have the link of this internal classes? Does it need to compile or its just a simple copy and paste?Gnawing
@Pedram As I mentioned in this solution..We are using internal APIs to achieve this so We have to build the application as system app and It takes all related imports of packages in compile...Thorny
T
2

i found way to get only wifi traffic

long totalbyte = Trafficstats.getTotalRxBytes();
long mobilenetworkbyte = Trafficstats.getMobileRxBytes();
String total = Long.toString(totalbyte);
String mobile = Long.toString(mobilenetworkbyte);
String wifibyte = total - mobile + "kb";

now wifibyte string show wifi total byte its work for me i hope that work for you

Topographer answered 25/12, 2014 at 18:38 Comment(0)
R
-2

Try the following code and turn off your 'WIFI' and check with only '3G'

  1. Create a new Android project in Eclipse. Remember to use the TrafficStats class you must target the API for Android 2.2 (Froyo) or higher.

  2. In the /res/layout folder we will create a main.xml resource. For this project, we are just using a series of text views in a vertically stacked linear layout.

          main.xml
    
         <?xml version="1.0" encoding="utf-8"?>
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
         android:layout_width="fill_parent"
    
         android:layout_height="fill_parent"
    
         android:orientation="vertical" >
    
         <TextView
    
         android:layout_width="fill_parent"
    
         android:layout_height="wrap_content"
    
         android:textSize="16sp"
    
         android:textStyle="bold"
    
         android:gravity="center"
    
         android:paddingBottom="20dip"
    
         android:text="Traffic Stats Demo" />
    
         <TextView
    
         android:layout_width="fill_parent"
    
         android:layout_height="wrap_content"
    
         android:textSize="14sp"
    
         android:textColor="#00ff00"
    
         android:gravity="center"
    
         android:text="Transmit Bytes" />
    
         <TextView
    
         android:layout_width="fill_parent"
    
         android:layout_height="wrap_content"
    
         android:textSize="14sp"
    
         android:gravity="center"
    
         android:text="0"
    
         android:id="@+id/TX"/>
    
        <TextView
    
        android:layout_width="fill_parent"
    
        android:layout_height="wrap_content"
    
        android:textSize="14sp"
    
        android:textColor="#ff0000"
    
        android:gravity="center"
    
        android:text="Receive Bytes" />
    
        <TextView
    
        android:layout_width="fill_parent"
    
        android:layout_height="wrap_content"
    
        android:textSize="14sp"
    
        android:gravity="center"
    
        android:text="0"
    
        android:id="@+id/RX"/>
    
        </LinearLayout>
    
  3. With our layout in place we can move on to the /src folder. Create Main.java by extending the Activity class. Let’s also go ahead and declare three private class variables.

Main.java

     package com.authorwjf;

     import android.app.Activity;

     import android.app.AlertDialog;

     import android.net.TrafficStats;

     import android.os.Bundle;

     import android.os.Handler;

     import android.widget.TextView;

     public class Main extends Activity {

     private Handler mHandler = new Handler();

     private long mStartRX = 0;

     private long mStartTX = 0;

      }
  1. We will use the on create override to initialize our private variables, as well as schedule a callback on the UI thread. Make a note of the check for the enum TrafficStats.UNSUPPORTED. While my experience with the TrafficStats class has been without a hitch, the official Google documentation states that some devices may not support this type of reporting and when that is the case the call returns the aforementioned value. For that reason it’s a good idea to write your code defensively, as I’ve demonstrated here.

          Main.java
    
         @Override
    
         public void onCreate(Bundle savedInstanceState) {
    
         super.onCreate(savedInstanceState);
    
         setContentView(R.layout.main);
    
         mStartRX = TrafficStats.getTotalRxBytes();
    
         mStartTX = TrafficStats.getTotalTxBytes();
    
         if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX ==     TrafficStats.UNSUPPORTED) {
    
          AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
          alert.setTitle("Uh Oh!");
    
         alert.setMessage("Your device does not support traffic stat monitoring.");
    
         alert.show();
    
         } else {
    
         mHandler.postDelayed(mRunnable, 1000);
    
         }
    
           }
    
  2. Last but not least we need to update our display and reschedule the runnable.

          Main.java
    
          private final Runnable mRunnable = new Runnable() {
    
          public void run() {
    
          TextView RX = (TextView)findViewById(R.id.RX);
    
           TextView TX = (TextView)findViewById(R.id.TX);
    
           long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
    
             RX.setText(Long.toString(rxBytes));
    
           long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
    
           TX.setText(Long.toString(txBytes));
    
            mHandler.postDelayed(mRunnable, 1000);
    
               }
    
               };
    
Resignation answered 27/9, 2012 at 4:8 Comment(7)
Thanks, but to get non-WiFi network traffic usage, I can just using TrafficStats.getMobileTxBytes() andTrafficStats.getMobileRxBytes(). Also, that can't get each APP of 3g usage, and that's what really I need.Garnish
actually it takes the whole phone traffic statistics, for application usage i m still trying to find the solution, if i get than i will post for youResignation
For this, I'm trying another solution that is: 1)Detect 3G turned ON 2)Start get traffic count per APP by using TrafficStats 3)Stop get traffic count when WiFi turned ON...........Another problem is what if 3G & WiFi both turned ON? Still figuring it out......Garnish
are you able to get TrafficStats per APP?Resignation
Yes, but the return value was a summary of 3G & WiFi count. So I decide to count myself when 3G been ON, and stop count when 3G off. But still don't know how to listen 3G ON/OFF event...Garnish
@RRTW: Actually, when both 3G and WiFi are on, the device will favor the WiFi. So, you can just reverse your logic: if WiFi is on, then getUidRxBytes(...) and getUidTxBytes(...) will return the WiFi traffic, the 3G otherwise.Prothorax
It's credit to techrepublic.com/blog/software-engineer/… :William J. FrancisNationalist

© 2022 - 2024 — McMap. All rights reserved.