很多安全卫士类软件都实现了网速监测功能,也算是一个比较实用的功能。Android下,TrafficStats类实现了对流量的统计。
/proc/uid_stat/uid/tcp_send 上传流量
/proc/uid_stat/uid/tcp_rcv 下载流量
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(intuid) //获取某个网络UID的发送字节数
获取某个应用的流量的统计(根据包名)
一、根据包名获取相应的UID;
/**
* 获取当前应用uid
* @return
*/
public int getUid() {
try {
PackageManager pm = mContext.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
return ai.uid;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
二、根据UID获取相应的流量的统计
TrafficInfo tr=new TrafficInfo(getApplicationContext());
int uid=tr.getUid();
Log.e("asdf",uid+"");
Log.e("asdf",tr.getRcvTraffic()+"--"+tr.getSndTraffic()+"-----"+tr.getTrafficInfo());
package com.rzt.qualitytest.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
* 应用的流量信息
*/
public class TrafficInfo {
private static final int UNSUPPORTED = -1;
private static final String LOG_TAG = "test";
private static TrafficInfo instance;
static int uid=0;
private long preRxBytes = 0;
private Timer mTimer = null;
/** 更新频率(每几秒更新一次,至少1秒) */
private final int UPDATE_FREQUENCY = 1;
private int times = 1;
private Context mContext;
public TrafficInfo(Context mContext, int uid) {
this.uid = uid;
this.mContext=mContext;
}
public TrafficInfo(Context mContext) {
this.mContext=mContext;
}
/**
* 获取总流量
*
* @return
*/
public long getTrafficInfo() {
long rcvTraffic = UNSUPPORTED; // 下载流量
long sndTraffic = UNSUPPORTED; // 上传流量
rcvTraffic = getRcvTraffic();
sndTraffic = getSndTraffic();
if (rcvTraffic == UNSUPPORTED || sndTraffic == UNSUPPORTED)
return UNSUPPORTED;
else
return rcvTraffic + sndTraffic;
}
/**
* 获取下载流量 某个应用的网络流量数据保存在系统的/proc/uid_stat/$UID/tcp_rcv | tcp_snd文件中
*
* @return
*/
public long getRcvTraffic() {
long rcvTraffic = UNSUPPORTED; // 下载流量
if(uid==0){
uid=getUid();
}
rcvTraffic = TrafficStats.getUidRxBytes(uid);
if (rcvTraffic == UNSUPPORTED) { // 不支持的查询
return UNSUPPORTED;
}
Log.i("test", rcvTraffic + "--1");
RandomAccessFile rafRcv = null, rafSnd = null; // 用于访问数据记录文件
String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv";
try {
rafRcv = new RandomAccessFile(rcvPath, "r");
rcvTraffic = Long.parseLong(rafRcv.readLine()); // 读取流量统计
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());
rcvTraffic = UNSUPPORTED;
} catch (IOException e) {
Log.e(LOG_TAG, "IOException: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (rafRcv != null)
rafRcv.close();
if (rafSnd != null)
rafSnd.close();
} catch (IOException e) {
Log.w(LOG_TAG, "Close RandomAccessFile exception: " + e.getMessage());
}
}
Log.i("test", rcvTraffic + "--2");
return rcvTraffic;
}
/**
* 获取上传流量
*
* @return
*/
public long getSndTraffic() {
long sndTraffic = UNSUPPORTED; // 上传流量
if(uid==0){
uid=getUid();
}
sndTraffic = TrafficStats.getUidTxBytes(uid);
if (sndTraffic == UNSUPPORTED) { // 不支持的查询
return UNSUPPORTED;
}
RandomAccessFile rafRcv = null, rafSnd = null; // 用于访问数据记录文件
String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd";
try {
rafSnd = new RandomAccessFile(sndPath, "r");
sndTraffic = Long.parseLong(rafSnd.readLine());
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());
sndTraffic = UNSUPPORTED;
} catch (IOException e) {
Log.e(LOG_TAG, "IOException: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (rafRcv != null)
rafRcv.close();
if (rafSnd != null)
rafSnd.close();
} catch (IOException e) {
Log.w(LOG_TAG, "Close RandomAccessFile exception: " + e.getMessage());
}
}
return sndTraffic;
}
/**
* 获取当前下载流量总和
*
* @return
*/
public static long getNetworkRxBytes() {
return TrafficStats.getTotalRxBytes();
}
/**
* 获取当前上传流量总和
*
* @return
*/
public static long getNetworkTxBytes() {
return TrafficStats.getTotalTxBytes();
}
/**
* 获取当前网速
*
* @return
*/
public double getNetSpeed() {
long curRxBytes = getNetworkRxBytes();
if (preRxBytes == 0)
preRxBytes = curRxBytes;
long bytes = curRxBytes - preRxBytes;
preRxBytes = curRxBytes;
//int kb = (int) Math.floor(bytes / 1024 + 0.5);
double kb = (double)bytes / (double)1024;
BigDecimal bd = new BigDecimal(kb);
return bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public void stopCalculateNetSpeed() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
/**
* 获取当前应用uid
* @return
*/
public int getUid() {
try {
PackageManager pm = mContext.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
return ai.uid;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
/**
* 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的发送字节数
*/
}
转载:http://blog.csdn.net/yyh352091626/article/details/50599621