4

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.

Ger Cas
  • 2,188
  • 2
  • 18
  • 45

1 Answers1

4

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

shb
  • 5,957
  • 2
  • 15
  • 32
  • Hi. Thanks so much for your help. I've tried how you said. Initially I had some issues but moving things here and there it worked. The only 2 issues I found so far is that on one phone and emulator ( both android 6) continue growing the RX and Tx even If I put on GSM or data off. In other Android 6 phone that issue didn't happen. The other issue is that the changes from disconnected to 2G, 3G etc are detected fine in Android 6 but when I test in Android 7 doesn't work. Maybe you can suggest me what to do for these issues or let me know if is better to open a new post. Thanks again – Ger Cas Sep 09 '19 at 07:12
  • Not entirely sure whats causing the issue you mentioned. Try searching SO or open a new question if needed. – shb Sep 09 '19 at 10:39