android开发之流量监控

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 ?
  1. public void dingshi(){  
  2.             
  3.                 mdb.openDB();//打开数据库  
  4.       
  5.                 runnableapp=new Runnable(){ //线程对象  
  6.                 @Override  
  7.                 public void run() {  
  8.                     while(flag==0){//用于停止线程时的判断  
  9.                         try {  
  10.                             Thread.sleep(1000);//每1000ms进行流量监控  
  11.                         } catch (InterruptedException e) {  
  12.                             // TODO Auto-generated catch block  
  13.                             e.printStackTrace();  
  14.                         }  
  15.                     int i=0;  
  16.                     Log.v("app""id---->"+Thread.currentThread().getId());  
  17.                     try{                                  
  18.                     for (i=0;i<listuser.size();i++) {//listuser想要监控的应用,只要列表不再重新选择,则获取应用的顺序一定  
  19.                         Log.v("app""开始"+Thread.currentThread().getId());  
  20.                         uid=Integer.parseInt(listuser.get(i));  //列表中第i个选择的应用的uid  
  21.                         appinfo=listappinfo.get(i);  
  22.                           
  23.                           Calendar calendar = Calendar.getInstance();  
  24.                             int month=calendar.get(Calendar.MONTH)+1;  
  25.                             int day=calendar.get(Calendar.DAY_OF_MONTH);  
  26.                             int hour=calendar.get(Calendar.HOUR_OF_DAY);  
  27.                             int minute=calendar.get(Calendar.MINUTE);  
  28.                             int second=calendar.get(Calendar.SECOND);  
  29.                             float mill=calendar.get(Calendar.MILLISECOND);  
  30.                              String time= calendar.get(Calendar.YEAR) + "年"  
  31.                                         + month + "月"  
  32.                                         + day + "日"  
  33.                                         + hour + "时"  
  34.                                         + minute + "分"  
  35.                                         +second+"秒"  
  36.                                         +mill+"毫秒";  
  37.                                
  38.                              time02[i]=day*24*60*60+hour*60*60+minute*60+second+mill/1000;//单位是秒   新获取的时间,肯定大于前面获取的时间  
  39.                                
  40.                         System.out.println("现在的时间"+i+"--->"+time02[i]);  
  41.                         System.out.println("之前的时间"+i+"--->"+time01[i]);  
  42.                           
  43.                         recv[i]=TrafficStats.getUidRxBytes(uid);  
  44.                         if(recv[i]>0) {  
  45.                         recv[i]=recv[i]/(1024*1024);//单位化为MB  
  46.                         }else{  
  47.                             recv[i]=0;  
  48.                         }  
  49.                         tran[i]=TrafficStats.getUidTxBytes(uid);  
  50.                         if(tran[i]>0){  
  51.                         tran[i]=tran[i]/(1024*1024);  
  52.                         }  
  53.                         else{  
  54.                             tran[i]=0;  
  55.                         }  
  56.                         totalapp[i]=recv[i]+tran[i];  
  57.                           
  58.                         recvrate[i]=((recv[i]-recv01[i])*1024*1024)/(time02[i]-time01[i]); //单位为B/s   //均为瞬时速率  
  59.                         tranrate[i]=((tran[i]-tran01[i])*1024*1024)/(time02[i]-time01[i]);  
  60.                         totalapprate[i]=((totalapp[i]-totalapp01[i])*1024*1024)/(time02[i]-time01[i]);  
  61.                           
  62.                         System.out.println(appinfo+i+"---->" +"接收速率---->"+recvrate[i]+ "发送速率---->" + tranrate[i]+"total速率---->"+totalapprate[i]);  
  63.                         System.out.println("时间间隔"+i+"---->"+(time02[i]-time01[i]));    
  64.                           
  65.                         recv01[i]=recv[i];//给全局变量赋新值  
  66.                         tran01[i]=tran[i];  
  67.                         totalapp01[i]=totalapp[i];  
  68.                         time01[i]=time02[i];    
  69.                                           
  70.                                       
  71.                         System.out.println("uid=" + uid +"---->""recv=" + recv[i]);   
  72.                         System.out.println("uid=" + uid +"---->""tran=" + tran[i]);  
  73.                         System.out.println("uid=" + uid +"---->"+ totalapp[i]);  
  74.                         System.out.println("appinfo=" + appinfo );  
  75.                        try{  
  76.                         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]));  
  77.                        }catch(Exception e){  
  78.                            System.out.println("----->app出现异常");  
  79.                        }  
  80.                         Log.v("app""结束"+Thread.currentThread().getId());  
  81.                     }  
  82.                     }catch(Exception e){  
  83.                         System.out.println("异常");  
  84.                     }  
  85.                                             
  86.                           
  87.                             }  
  88.                 }  
  89.                             };  
  90.                             Thread AppThread = new Thread(runnableapp);    
  91.                             AppThread.start();  
  92.                               
  93.                           
  94.                     }  
[java] view plain copy print ?

你可能感兴趣的:(thread,exception,android,数据库,calendar,mobile)