How to count separately data usage in 2G and 3G with TrafficStats?

124 views Asked by At

How can I mix/join the following two codes in order to count TX and RX bytes separately when the phone is in 2G or 3G?

I have the following code to identify when the phone is connected to EDGE or UMTS network that seems to work, showing a Toast message on display.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener ConnectionStateListener = new PhoneStateListener() {

            @Override
            public void onDataConnectionStateChanged(int state, int networkType) {
                super.onDataConnectionStateChanged(state, networkType);
                String sState = "";

                switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_EDGE:   sState = "EDGE (2G)";   break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:   sState = "UMTS (3G)";   break;                    
                }
                Toast.makeText(getApplicationContext(), sState, Toast.LENGTH_SHORT).show();
            }
        };
        telephonyManager.listen(ConnectionStateListener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    }
}

From this example in Tech Republic I get this code to count the TX and RX bytes.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStartRX = TrafficStats.getTotalRxBytes();
        mStartTX = TrafficStats.getTotalTxBytes();

        mHandler.postDelayed(mRunnable, 1000);
    }

    //This would be to update the bytes usage every second
    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);
        }
        };
}

Maybe someone could help me with this. Thanks in advance for any advice/help.

1

There are 1 answers

2
shb On BEST ANSWER

Correct me if I perceived your question wrong.

You want to count TX & RX separately for 2G and 3G data.

Copy necessary code to a single activity.

Take a Boolean is3G and change it onDataConnectionStateChanged

then trigger mRunnable

telephonyManager.listen(ConnectionStateListener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);

mHandler.postDelayed(mRunnable, 1000);

Measure rx/tx bytes separately for 2G and 3G inside mRunnable

 if(is3G) {
      rxBytes3G = TrafficStats.getTotalRxBytes() - mStartRX - rxBytes2G;
      txBytes3G = TrafficStats.getTotalTxBytes() - mStartTX - txBytes2G;
  } else {
      rxBytes2G = TrafficStats.getTotalRxBytes() - mStartRX - rxBytes3G;
      txBytes2G  = TrafficStats.getTotalTxBytes() - mStartTX - txBytes3G;
  }

Update your ui