Android流量监控

1、TrafficStats类的使用

以下结论,是自己真机实测的结果,与自己在网上看到的不同,大家可自测验证。

(1)getMobile...方法,获取Gprs/3G流量

(2)getTotal...方法,获取Gprs/3G+Wifi的流量

以上两类方法统计的都是,从打开网络开始,到关闭网络,这一段时间内使用的流量。例如:10点打开,11点关闭,那么getMobileRxBytes方法,回返的是这段时间内Gprs/3G接受到的字节数。

(3)getUid...方法,获取某个网络UID的流量。这类方法,返回的是,从开机到关机,某个网络UID(我理解就是某个应用,如果不对,请在评论中指正)使用的Gprs/3G+Wifi的流量


2、getUid...方法的使用

以下这段代码是网上找的,出处找不到了,希望原作者不要介意啊

  1. List<PackageInfo> packinfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_PERMISSIONS);  
  2. for (PackageInfo info : packinfos) {  
  3.     String[] premissions = info.requestedPermissions;  
  4.     if (premissions != null && premissions.length > 0) {  
  5.         for (String premission : premissions) {  
  6.             if ("android.permission.INTERNET".equals(premission)) {  
  7.                 // System.out.println(info.packageName+"访问网络");  
  8.                 int uid = info.applicationInfo.uid;  
  9.                 long rx = TrafficStats.getUidRxBytes(uid);  
  10.                 long tx = TrafficStats.getUidTxBytes(uid);  
  11.                 if (rx < 0 || tx < 0) {  
  12.                     System.out.println(info.packageName + "没有产生流量");  
  13.                 } else {  
  14.                     System.out.println(info.packageName + "的流量信息:");  
  15.                     System.out.println("下载的流量" + Formatter.formatFileSize(this, rx));  
  16.                     System.out.println("上传的流量" + Formatter.formatFileSize(this, tx));  
  17.                 }  
  18.             }  
  19.         }  
  20.         System.out.println("---------");  
  21.     }  
  22. }  

3、获取流量信息的Service类

(1)因为两个广播都需要动态注册,所以写成了Service

(2)getMobile...和getTotal...方法获取的都是从打开网络开始,到关闭网络,这一段时间内使用的流量。

因此要在网络正在关闭时获取的就是这段时间的流量,WifiManager.WIFI_STATE_DISABLING表示的就是这个状态

(3)Gprs/3G貌似没有类似的状态,可以被监控到,只有,State.CONNECTED和State.DISCONNECTED。但是处于State.DISCONNECTED这个状态时,getMobile...方法获取到的值就都是0了

所有,我不得不在State.CONNECTED这个状态开始时,开启线程用于获取Gprs/3G的流量,直到我获取的到数据为0时,保存上一次的数据。

如果大家有更好的方法,请务必告诉我啊!

  1. package forrest.forassist.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.net.ConnectivityManager;  
  9. import android.net.NetworkInfo;  
  10. import android.net.NetworkInfo.State;  
  11. import android.net.TrafficStats;  
  12. import android.net.wifi.WifiManager;  
  13. import android.os.IBinder;  
  14. import android.text.format.Formatter;  
  15. import android.util.Log;  
  16. import forrest.forassist.db.MySQLiteDatabase;  
  17. import forrest.forassist.utils.Util;  
  18.   
  19. public class TrafficService extends Service {  
  20.   
  21.     private TrafficReceiver tReceiver;  
  22.     private WifiManager wifiManager;  
  23.     private ConnectivityManager cManager;  
  24.   
  25.     public IBinder onBind(Intent intent) {  
  26.         return null;  
  27.     }  
  28.   
  29.     public void onCreate() {  
  30.         // WifiManager,ConnectivityManager  
  31.         wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);  
  32.         cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);  
  33.         // 注册TrafficReceiver  
  34.         tReceiver = new TrafficReceiver();  
  35.         IntentFilter filter = new IntentFilter();  
  36.         filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);  
  37.         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  38.         registerReceiver(tReceiver, filter);  
  39.         super.onCreate();  
  40.     }  
  41.   
  42.     public int onStartCommand(Intent intent, int flags, int startId) {  
  43.   
  44.         return super.onStartCommand(intent, flags, startId);  
  45.     }  
  46.   
  47.     private class TrafficReceiver extends BroadcastReceiver {  
  48.         private String action = "";  
  49.         private static final String TAG = "TrafficReceiver";  
  50.         long mobileRx;  
  51.         long mobileTx;  
  52.   
  53.         public void onReceive(Context context, Intent intent) {  
  54.             action = intent.getAction();  
  55.             if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {  
  56.                 if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLING) {  
  57.                     Log.i(TAG, "WIFI_STATE_DISABLING");  
  58.                     long wifiDown = TrafficStats.getTotalRxBytes() - TrafficStats.getMobileRxBytes();  
  59.                     long wifiUp = TrafficStats.getTotalTxBytes() - TrafficStats.getMobileTxBytes();  
  60.                     MySQLiteDatabase sqLite = new MySQLiteDatabase(context); // 打开数据库  
  61.                     sqLite.insertWifi(Util.todayDate, wifiDown, wifiUp);  
  62.                     sqLite.closeDB();  
  63.                     Log.i(TAG, "wifi下载流量" + Formatter.formatFileSize(context, wifiDown));  
  64.                     Log.i(TAG, "wifi上传流量" + Formatter.formatFileSize(context, wifiUp));  
  65.                 }  
  66.             } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {  
  67.                 Log.i(TAG, "CONNECTIVITY_ACTION");  
  68.                 NetworkInfo networkInfo = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  69.                 State state = networkInfo.getState();  
  70.                 if (state == State.CONNECTED) {  
  71.                     Log.i(TAG, "State.CONNECTED");  
  72.                     // 开始不断获取最近的流量信息,值为0时,跳过  
  73.                     new Thread() {  
  74.                         public void run() {  
  75.                             long mobileRxType = TrafficStats.getMobileRxBytes();  
  76.                             long mobileTxType = TrafficStats.getMobileTxBytes();  
  77.                             if (mobileRxType + mobileTxType != 0) {  
  78.                                 try {  
  79.                                     mobileRx = mobileRxType;  
  80.                                     mobileTx = mobileTxType;  
  81.                                     Log.i(TAG, "mobileRx:" + mobileRx);  
  82.                                     Log.i(TAG, "mobileTx:" + mobileTx);  
  83.                                     Thread.sleep(1000);  
  84.                                     run();  
  85.                                 } catch (InterruptedException e) {  
  86.                                     e.printStackTrace();  
  87.                                 }  
  88.                             } else {  
  89.                                 // 写入数据库  
  90.                                 Log.i(TAG, "写入数据库");  
  91.                                 MySQLiteDatabase sqLite = new MySQLiteDatabase(TrafficService.this);  
  92.                                 sqLite.insertGprs(Util.todayDate, mobileRx, mobileTx);  
  93.                                 sqLite.closeDB();  
  94.                             }  
  95.                         };  
  96.                     }.start();  
  97.                 }   
  98.             }  
  99.         }  
  100.     }  
  101.   
  102.     public void onDestroy() {  
  103.         unregisterReceiver(tReceiver);  
  104.         super.onDestroy();  
  105.     }  


来源:http://blog.youkuaiyun.com/wo334499/article/details/8557256


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值