Android trafficstats的用法

来源:互联网 发布:图片制作成视频软件 编辑:程序博客网 时间:2024/06/05 22:57

当我们想知道应用联网所需要的的流量时,一般会用到trafficstats类的方法。

trafficstats类有以下公共方法:

static voidclearThreadStatsTag()

Clear any active tag set to account Socket traffic originating from the current thread.
static longgetMobileRxBytes()
Return number of bytes received across mobile networks since device boot.
static longgetMobileRxPackets()
Return number of packets received across mobile networks since device boot.
static longgetMobileTxBytes()
Return number of bytes transmitted across mobile networks since device boot.
static longgetMobileTxPackets()
Return number of packets transmitted across mobile networks since device boot.
static intgetThreadStatsTag()
Get the active tag used when accounting Socket traffic originating from the current thread.
static longgetTotalRxBytes()
Return number of bytes received since device boot.
static longgetTotalRxPackets()
Return number of packets received since device boot.
static longgetTotalTxBytes()
Return number of bytes transmitted since device boot.
static longgetTotalTxPackets()
Return number of packets transmitted since device boot.
static longgetUidRxBytes(int uid)
Return number of bytes received by the given UID since device boot.
static longgetUidRxPackets(int uid)
Return number of packets received by the given UID since device boot.
static longgetUidTcpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidTcpRxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidTcpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidTcpTxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidTxBytes(int uid)
Return number of bytes transmitted by the given UID since device boot.
static longgetUidTxPackets(int uid)
Return number of packets transmitted by the given UID since device boot.
static longgetUidUdpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidUdpRxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidUdpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static longgetUidUdpTxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always returnUNSUPPORTED.
static voidincrementOperationCount(int tag, int operationCount)
Increment count of network operations performed under the given accounting tag.
static voidincrementOperationCount(int operationCount)
Increment count of network operations performed under the accounting tag currently active on the calling thread.
static voidsetThreadStatsTag(int tag)
Set active tag to use when accounting Socket traffic originating from the current thread.
static voidtagSocket(Socket socket)
Tag the given Socket with any statistics parameters active for the current thread.
static voiduntagSocket(Socket socket)
Remove any statistics parameters from the given Socket.
常用的有以下方法

static long  getMobileRxBytes()  //获取通过Mobile连接收到的字节总数,不包含WiFi  
static long  getMobileRxPackets()  //获取Mobile连接收到的数据包总数  
static long  getMobileTxBytes()  //Mobile发送的总字节数  
static long  getMobileTxPackets()  //Mobile发送的总数据包数  
static long  getTotalRxBytes()  //获取总的接受字节数,包含Mobile和WiFi等  
static long  getTotalRxPackets()  //总的接受数据包数,包含Mobile和WiFi等  
static long  getTotalTxBytes()  //总的发送字节数,包含Mobile和WiFi等  
static long  getTotalTxPackets()  //发送的总数据包数,包含Mobile和WiFi等   
static long  getUidRxBytes(int uid)  //获取某个网络UID的接受字节数  
static long  getUidTxBytes(int uid) //获取某个网络UID的发送字节数   
总接受流量TrafficStats.getTotalRxBytes(), 
总发送流量TrafficStats.getTotalTxBytes())
但是Android4.2.2及Android4.3却获取不到

先看一个例子

PackageManager manager = mContext.getPackageManager();        ArrayList<PackageInfo> appInfoList = getAllApps();        for (PackageInfo info : appInfoList) {            ApplicationInfo applicationInfo = info.applicationInfo;            String pkg = info.packageName;            String app = manager.getApplicationLabel(applicationInfo)                    .toString();            int uid = info.applicationInfo.uid;            long rx = TrafficStats.getUidRxBytes(uid);            long tx = TrafficStats.getUidTxBytes(uid);             Log.i("tag",                    "-----------------------uid=" + uid + "--rx=" + rx                            + "--tx=" + tx + "---rx1="                            + TrafficStats.getTotalRxBytes()/1024 + "---tx1="                            + TrafficStats.getTotalTxBytes()/1024);            }

public ArrayList<PackageInfo> getAllApps() {ArrayList<PackageInfo> appList = new ArrayList<PackageInfo>();PackageManager pm = mContext.getPackageManager();List<PackageInfo> appList_temp = pm.getInstalledPackages(0);for (int i = 0; i < appList_temp.size(); i++) {PackageInfo pak = appList_temp.get(i);// 过滤系统的程序// if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=// 0) {// appList.add(pak);// }if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {appList.add(pak);}}appList_temp.clear();return appList;}


在Android4.3上面,是0.

但是我们可以用以下方法获取

private Long getTotalBytesManual(int localUid){File dir = new File("/proc/uid_stat/");String[] children = dir.list();if(!Arrays.asList(children).contains(String.valueOf(localUid))){    return 0L;}File uidFileDir = new File("/proc/uid_stat/"+String.valueOf(localUid));File uidActualFileReceived = new File(uidFileDir,"tcp_rcv");File uidActualFileSent = new File(uidFileDir,"tcp_snd"); String textReceived = "0"; String textSent = "0"; try {        BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));        BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent));        String receivedLine;        String sentLine;        if ((receivedLine = brReceived.readLine()) != null) {            textReceived = receivedLine;        }        if ((sentLine = brSent.readLine()) != null) {            textSent = sentLine;        }    }    catch (IOException e) {    } return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();}




0 0
原创粉丝点击