Android中进行流量统计

来源:互联网 发布:unity3d 体素 编辑:程序博客网 时间:2024/05/01 01:23
// ---------------------流量统计--------------------------------try {PackageManager pm = getPackageManager();ApplicationInfo ai = pm.getApplicationInfo("com.test.app",PackageManager.GET_ACTIVITIES);// com.test.app为自己应用的包名Log.d("!!", "!!" + ai.uid);long received = TrafficStats.getUidRxBytes(ai.uid);// 获取某个网络UID的接受字节数long sent = TrafficStats.getUidTxBytes(ai.uid);// 获取某个网络UID的发送字节数Toast.makeText(mContext, "累计接收数据" + received / 1024 + "KB",Toast.LENGTH_SHORT).show();Toast.makeText(mContext, "累计发送字节" + sent / 1024 + "KB",Toast.LENGTH_SHORT).show();} catch (NameNotFoundException e) {e.printStackTrace();}// -----------------------------------------------------
1 0