简体   繁体   English

android network stats:维护有关打包发送/接收信息的简便方法

[英]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. 我有一个Android应用程序,用于监控通过网络接收/传输的字节/数据包。 I show these informations in a textview: 我在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: 然后通过Java代码:

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: 而mRunnable的运行方法是:

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??? 问题是,当我恢复活动时,必须“调用”处理程序,并且字节数从0重新开始。如何在活动的整个生命周期内以简单的方式跟踪值?

通过将mstart值保存到共享的首选项中,然后将它们恢复为“ onResume”来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM