2012-03-06 84 views
1

我知道我可以使用trafficstats api的以下方法获取任何给定应用程序的所有网络接口的字节发送和接收。但是,如何仅通过移动设备获取统计信息?这样我就可以分离手机和wifi数据。TrafficStats特定应用程序特定的网络接口

getUidTxBytes(INT UID)
获取通过网络 此UID发送的字节的数目。

getUidRxBytes(int uid)
获取通过此UID通过 网络接收到的字节数。

看起来像这样的东西是从API中缺少的。和其他方式得到它 getUidMobileRxBytes(int uid)

回答

0

对不起,但你只能得到每个接口统计的整个设备,而不是每个UID。

您可以通过收集全接口,每UID统计信息和每接口全设备统计信息来估计每个接口的每个UID统计信息,然后假设整个设备的接口比率大约是该单个UID的比率。对于短时间跨度,这可能相当准确,因为设备只会定期切换接口。

1
package com.formation.traficstates; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.net.TrafficStats; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 

public class TraficstatesActivity extends Activity { 

    private Handler mHandler=new Handler();; 

    private long mStartRX; 





    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     mStartRX=TrafficStats.getTotalRxBytes(); 

     if (mStartRX == TrafficStats.UNSUPPORTED) { 

      AlertDialog.Builder alert = new AlertDialog.Builder(this); 

      alert.setTitle("Uh Oh!"); 

      alert.setMessage("Your device does not support traffic stat monitoring."); 

      alert.show(); 

     } else { 

      mHandler.postDelayed(mRunnable, 1000); 

     } 
    } 

    private final Runnable mRunnable = new Runnable() { 

     public void run() { 


     long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX; 

     TextView RX = (TextView)findViewById(R.id.textViewRX); 

     RX.setText("\nbytes recreived :"+Long.toString(rxBytes)); 


     } 

     }; 

} 
相关问题