Android trafficstats的用法
来源:互联网 发布:图片制作成视频软件 编辑:程序博客网 时间:2024/06/05 22:57
当我们想知道应用联网所需要的的流量时,一般会用到trafficstats类的方法。
trafficstats类有以下公共方法:
Clear any active tag set to account
Socket
traffic originating from the current thread.Return number of bytes received across mobile networks since device boot.
Return number of packets received across mobile networks since device boot.
Return number of bytes transmitted across mobile networks since device boot.
Return number of packets transmitted across mobile networks since device boot.
Get the active tag used when accounting
Socket
traffic originating from the current thread.Return number of bytes received since device boot.
Return number of packets received since device boot.
Return number of bytes transmitted since device boot.
Return number of packets transmitted since device boot.
Return number of bytes received by the given UID since device boot.
Return number of packets received by the given UID since device boot.
This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.Return number of bytes transmitted by the given UID since device boot.
Return number of packets transmitted by the given UID since device boot.
This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.This method was deprecated in API level 18. Starting in
JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always returnUNSUPPORTED
.Increment count of network operations performed under the given accounting tag.
Increment count of network operations performed under the accounting tag currently active on the calling thread.
Set active tag to use when accounting
Socket
traffic originating from the current thread.Tag the given
Socket
with any statistics parameters active for the current thread.Remove any statistics parameters from the given
常用的有以下方法Socket
.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的发送字节数
总接受流量TrafficStats.getTotalRxBytes(),
总发送流量TrafficStats.getTotalTxBytes())
但是Android4.2.2及Android4.3却获取不到先看一个例子
PackageManager manager = mContext.getPackageManager(); ArrayList<PackageInfo> appInfoList = getAllApps(); for (PackageInfo info : appInfoList) { ApplicationInfo applicationInfo = info.applicationInfo; String pkg = info.packageName; String app = manager.getApplicationLabel(applicationInfo) .toString(); int uid = info.applicationInfo.uid; long rx = TrafficStats.getUidRxBytes(uid); long tx = TrafficStats.getUidTxBytes(uid); Log.i("tag", "-----------------------uid=" + uid + "--rx=" + rx + "--tx=" + tx + "---rx1=" + TrafficStats.getTotalRxBytes()/1024 + "---tx1=" + TrafficStats.getTotalTxBytes()/1024); }
public ArrayList<PackageInfo> getAllApps() {ArrayList<PackageInfo> appList = new ArrayList<PackageInfo>();PackageManager pm = mContext.getPackageManager();List<PackageInfo> appList_temp = pm.getInstalledPackages(0);for (int i = 0; i < appList_temp.size(); i++) {PackageInfo pak = appList_temp.get(i);// 过滤系统的程序// if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=// 0) {// appList.add(pak);// }if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {appList.add(pak);}}appList_temp.clear();return appList;}
在Android4.3上面,是0.
但是我们可以用以下方法获取
private Long getTotalBytesManual(int localUid){File dir = new File("/proc/uid_stat/");String[] children = dir.list();if(!Arrays.asList(children).contains(String.valueOf(localUid))){ return 0L;}File uidFileDir = new File("/proc/uid_stat/"+String.valueOf(localUid));File uidActualFileReceived = new File(uidFileDir,"tcp_rcv");File uidActualFileSent = new File(uidFileDir,"tcp_snd"); String textReceived = "0"; String textSent = "0"; try { BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived)); BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent)); String receivedLine; String sentLine; if ((receivedLine = brReceived.readLine()) != null) { textReceived = receivedLine; } if ((sentLine = brSent.readLine()) != null) { textSent = sentLine; } } catch (IOException e) { } return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();}
0 0
- Android trafficstats的用法
- Android TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- Android流量统计TrafficStats类的使用
- NYOJ-116士兵杀敌(二)
- VS2010/MFC编程入门之二(VS2010应用程序工程中文件的组成结构)
- MYSQL导入导出命令详解
- java类加载原理
- 利用反射动态生成sql语句
- Android trafficstats的用法
- WinMain win32 Application
- 将你的Mac设置为共享的Wifi热点
- Spring声明式事务的四种方式
- 图像数据预处理(上)
- Java 重写(Override)与重载(Overload)
- VS2010 常用快捷键
- java自动类型转换
- SSDP 简单服务发现协议