user1071138
user1071138

Reputation: 666

android network stats: easy way to mantain info about packed transmitted/received

I've an android application to monitor the bytes/packets received/transmitted throug the network. I show these informations in a textview:

<TextView
            android:id="@+id/networkInfo"
            android:layout_below="@id/toggle_apn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="20dp"
            android:background="@drawable/back"
            android:gravity="left"
            android:textSize="25dip"
            android:text="Not connected" />

and then via java code:

mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
mStartPRX=TrafficStats.getTotalRxPackets();
mStartPTX=TrafficStats.getTotalTxPackets();

if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
    ...
}else{
    mHandler.postDelayed(mRunnable, 1000);
}

and mRunnable run method is:

public void run() {
    TextView RX = (TextView)findViewById(R.id.networkInfo);
    long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
long rxPackets=TrafficStats.getTotalRxPackets()-mStartPRX;
    long txPackets=TrafficStats.getTotalTxPackets()-mStartPTX;

    RX.setText("Here I put the data above");
mHandler.postDelayed(mRunnable, 1000);
}

The problem is that, when I resume my activity, I have to "recall" the handler, and the number of bytes restart from 0. How can I trace in a easy way the values during the whole life of my activity???

Upvotes: 0

Views: 199

Answers (1)

user1071138
user1071138

Reputation: 666

Solved by saving mstart values into shared preferences and then resuming them into "onResume".

Upvotes: 0

Related Questions