android network stats: easy way to mantain info about packed transmitted/received -
i've android application monitor bytes/packets received/transmitted throug network. show these informations in 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 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 put data above"); mhandler.postdelayed(mrunnable, 1000); } the problem that, when resume activity, have "recall" handler, , number of bytes restart 0. how can trace in easy way values during whole life of activity???
solved saving mstart values shared preferences , resuming them "onresume".
Comments
Post a Comment