android statistic 3g traffic for each APP, how?

我只是一个虾纸丫 提交于 2019-11-28 18:24:53
RRTW

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.

Anshuman

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

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

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);
    
               }
    
               };
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!