Android:如何统计网络数据包?

时间:2013-06-18 21:35:15

标签: android android-networking

如何计算Android设备上的网络数据包输入/输出数量?

我需要这样做,以便我的应用程序在500个数据包(输入/输出)中加1 +

我正在使用Eclipse。

1 个答案:

答案 0 :(得分:0)

在阅读Michal评论后,我为TrafficStats课程提供资金。

https://developer.android.com/reference/android/net/TrafficStats.html

    new TrafficStats().getTotalTxPackets();

以下是我的应用现在的样子。

public class MainActivity extends Activity {
    private TrafficStats ts;
    private long totalTNumberOfPackets;
    private TextView tv_numberOfPackets;
    private NotificationManager mgr;
    private WebView wv;

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

        if(!(totalTNumberOfPackets == TrafficStats.UNSUPPORTED))
        {
            mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            wv = (WebView) findViewById(R.id.wv);
            wv.loadUrl("file:///android_asset/info.html");
            intiNotification();

            new Thread(
                    new Runnable() {
                        public void run() {
                            while(true)
                            {
                                intiNotification();
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                // Starts the thread by calling the run() method in its Runnable
                ).start();
        }
        else
        {
            Toast.makeText(this, "Hov.. din Android understøtter ikke denne app!!", Toast.LENGTH_LONG).show();
        }
    }
    public static long round(Long unrounded, int precision, int roundingMode)
    {
        BigDecimal bd = new BigDecimal(unrounded);
        BigDecimal rounded = bd.setScale(precision, roundingMode);
        return rounded.longValue();
    }
    public Long usersTotalNumberOfLogginger()
    {
        return round(new TrafficStats().getTotalTxPackets()/500, 0, BigDecimal.ROUND_HALF_DOWN);
    }
    @SuppressWarnings("deprecation")
    private void intiNotification() {
            Notification note = new Notification(R.drawable.ic_launcher,
                    "Logningstæller ("+usersTotalNumberOfLogginger()+" gange)", System.currentTimeMillis());
            PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(
                    this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);
            note.setLatestEventInfo(this, "Logningstæller", "Du er blivet logget "+String.valueOf(usersTotalNumberOfLogginger())+" gange",
                    i);
            // note.number = ++count;
            note.flags |= Notification.FLAG_ONGOING_EVENT;
            mgr.notify(0, note);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}