1. android实现流量监控在2.2版本之前没有专门的类和接口,我网上查看的大家的方法基本上都是读取一个文件/proc/net/dev,这个方法我没有使用,没有进行深入研究。
2. 我是用的是android2.2及之后版本有的流量监控类,TrafficStats,这个类里提供了以下几种方法,
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的发送字节数
由于项目中需要用到监控各个应用的流量,我的思路是,获取手机中所有具有联网权限的应用,并且以列表形式显示出来,然后选择相应的应用,即可对其进行流量监控。
这里用到的便是 getUidRxBytes(int uid)和 getUidTxBytes(int uid) 这两个方法。在我获取手机中所有具有联网权限的应用之前,我是把手机中所有应用的流量使用情况都显示了出来,我发现,连拨号器这种都有流量,最后我得出结论,这两个类方法获取的流量包括本地流量,所以就会出现连没有联网权限的应用都发送和接收字节数这种情况。
下面上一段我用于流量监控的代码。
[java] view plain copy print ?
- public void dingshi(){
-
- mdb.openDB();
-
- runnableapp=new Runnable(){
- @Override
- public void run() {
- while(flag==0){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- int i=0;
- Log.v("app", "id---->"+Thread.currentThread().getId());
- try{
- for (i=0;i<listuser.size();i++) {
- Log.v("app", "开始"+Thread.currentThread().getId());
- uid=Integer.parseInt(listuser.get(i));
- appinfo=listappinfo.get(i);
-
- Calendar calendar = Calendar.getInstance();
- int month=calendar.get(Calendar.MONTH)+1;
- int day=calendar.get(Calendar.DAY_OF_MONTH);
- int hour=calendar.get(Calendar.HOUR_OF_DAY);
- int minute=calendar.get(Calendar.MINUTE);
- int second=calendar.get(Calendar.SECOND);
- float mill=calendar.get(Calendar.MILLISECOND);
- String time= calendar.get(Calendar.YEAR) + "年"
- + month + "月"
- + day + "日"
- + hour + "时"
- + minute + "分"
- +second+"秒"
- +mill+"毫秒";
-
- time02[i]=day*24*60*60+hour*60*60+minute*60+second+mill/1000;
-
- System.out.println("现在的时间"+i+"--->"+time02[i]);
- System.out.println("之前的时间"+i+"--->"+time01[i]);
-
- recv[i]=TrafficStats.getUidRxBytes(uid);
- if(recv[i]>0) {
- recv[i]=recv[i]/(1024*1024);
- }else{
- recv[i]=0;
- }
- tran[i]=TrafficStats.getUidTxBytes(uid);
- if(tran[i]>0){
- tran[i]=tran[i]/(1024*1024);
- }
- else{
- tran[i]=0;
- }
- totalapp[i]=recv[i]+tran[i];
-
- recvrate[i]=((recv[i]-recv01[i])*1024*1024)/(time02[i]-time01[i]);
- tranrate[i]=((tran[i]-tran01[i])*1024*1024)/(time02[i]-time01[i]);
- totalapprate[i]=((totalapp[i]-totalapp01[i])*1024*1024)/(time02[i]-time01[i]);
-
- System.out.println(appinfo+i+"---->" +"接收速率---->"+recvrate[i]+ "发送速率---->" + tranrate[i]+"total速率---->"+totalapprate[i]);
- System.out.println("时间间隔"+i+"---->"+(time02[i]-time01[i]));
-
- recv01[i]=recv[i];
- tran01[i]=tran[i];
- totalapp01[i]=totalapp[i];
- time01[i]=time02[i];
-
-
- System.out.println("uid=" + uid +"---->"+ "recv=" + recv[i]);
- System.out.println("uid=" + uid +"---->"+ "tran=" + tran[i]);
- System.out.println("uid=" + uid +"---->"+ totalapp[i]);
- System.out.println("appinfo=" + appinfo );
- try{
- mdb.addTrafficData(time, appinfo, String.valueOf(uid), String.valueOf(tran[i]),String.valueOf(recv[i]),String.valueOf(totalapp[i]),String.valueOf(tranrate[i]),String.valueOf(recvrate[i]),String.valueOf(totalapprate[i]));
- }catch(Exception e){
- System.out.println("----->app出现异常");
- }
- Log.v("app", "结束"+Thread.currentThread().getId());
- }
- }catch(Exception e){
- System.out.println("异常");
- }
-
-
- }
- }
- };
- Thread AppThread = new Thread(runnableapp);
- AppThread.start();
-
-
- }
public void dingshi(){
mdb.openDB();//打开数据库
runnableapp=new Runnable(){ //线程对象
@Override
public void run() {
while(flag==0){//用于停止线程时的判断
try {
Thread.sleep(1000);//每1000ms进行流量监控
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=0;
Log.v("app", "id---->"+Thread.currentThread().getId());
try{
for (i=0;i<listuser.size();i++) {//listuser想要监控的应用,只要列表不再重新选择,则获取应用的顺序一定
Log.v("app", "开始"+Thread.currentThread().getId());
uid=Integer.parseInt(listuser.get(i)); //列表中第i个选择的应用的uid
appinfo=listappinfo.get(i);
Calendar calendar = Calendar.getInstance();
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
float mill=calendar.get(Calendar.MILLISECOND);
String time= calendar.get(Calendar.YEAR) + "年"
+ month + "月"
+ day + "日"
+ hour + "时"
+ minute + "分"
+second+"秒"
+mill+"毫秒";
time02[i]=day*24*60*60+hour*60*60+minute*60+second+mill/1000;//单位是秒 新获取的时间,肯定大于前面获取的时间
System.out.println("现在的时间"+i+"--->"+time02[i]);
System.out.println("之前的时间"+i+"--->"+time01[i]);
recv[i]=TrafficStats.getUidRxBytes(uid);
if(recv[i]>0) {
recv[i]=recv[i]/(1024*1024);//单位化为MB
}else{
recv[i]=0;
}
tran[i]=TrafficStats.getUidTxBytes(uid);
if(tran[i]>0){
tran[i]=tran[i]/(1024*1024);
}
else{
tran[i]=0;
}
totalapp[i]=recv[i]+tran[i];
recvrate[i]=((recv[i]-recv01[i])*1024*1024)/(time02[i]-time01[i]); //单位为B/s //均为瞬时速率
tranrate[i]=((tran[i]-tran01[i])*1024*1024)/(time02[i]-time01[i]);
totalapprate[i]=((totalapp[i]-totalapp01[i])*1024*1024)/(time02[i]-time01[i]);
System.out.println(appinfo+i+"---->" +"接收速率---->"+recvrate[i]+ "发送速率---->" + tranrate[i]+"total速率---->"+totalapprate[i]);
System.out.println("时间间隔"+i+"---->"+(time02[i]-time01[i]));
recv01[i]=recv[i];//给全局变量赋新值
tran01[i]=tran[i];
totalapp01[i]=totalapp[i];
time01[i]=time02[i];
System.out.println("uid=" + uid +"---->"+ "recv=" + recv[i]);
System.out.println("uid=" + uid +"---->"+ "tran=" + tran[i]);
System.out.println("uid=" + uid +"---->"+ totalapp[i]);
System.out.println("appinfo=" + appinfo );
try{
mdb.addTrafficData(time, appinfo, String.valueOf(uid), String.valueOf(tran[i]),String.valueOf(recv[i]),String.valueOf(totalapp[i]),String.valueOf(tranrate[i]),String.valueOf(recvrate[i]),String.valueOf(totalapprate[i]));
}catch(Exception e){
System.out.println("----->app出现异常");
}
Log.v("app", "结束"+Thread.currentThread().getId());
}
}catch(Exception e){
System.out.println("异常");
}
}
}
};
Thread AppThread = new Thread(runnableapp);
AppThread.start();
}
[java] view plain copy print ?