700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android 关于2.2版本之前的流量统计

android 关于2.2版本之前的流量统计

时间:2023-01-03 04:45:14

相关推荐

android 关于2.2版本之前的流量统计

不是原帖的原帖地址:/forum.php?mod=viewthread&tid=29&extra=page%3D3&page=1

2.2以前流量统计可以使用

package com.AAJM;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import android.util.Log;

public class TrafficStatsLL {

/**

* 获取网络流量信息

* 利用读取系统文件的方法来进行获取网络流量

* 主要意义在于可以应用于2.2以前的没有提供TrafficStats接口的版本

* @author cxy

* @Date -8-4

*/

public static String readInStream(FileInputStream inStream){

try {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int length = -1;

while((length = inStream.read(buffer)) != -1 ){

outStream.write(buffer, 0, length);

}

outStream.close();

inStream.close();

return outStream.toString();

} catch (IOException e){

Log.i("FileTest", e.getMessage());

}

return null;

}

获取手机2G/3G的下载流量//

public static long getMobileRxBytes()

{

long ReturnLong=0; //查询到的结果

try {

File file = new File("/proc/net/dev");

FileInputStream inStream = new FileInputStream(file);

String a=readInStream(inStream);

int startPos=a.indexOf("rmnet0:");

a=a.substring(startPos);

Pattern p=pile(" \\d+ ");

Matcher m=p.matcher(a);

while(m.find()){

ReturnLong=Long.parseLong(m.group().trim());

break;

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

return ReturnLong;

}

获取手机2G/3G的上传流量//

public static long getMobileTxBytes()

{

long ReturnLong=0; //查询到的结果

try {

int count=0; //返回结果时的计数器

File file = new File("/proc/net/dev");

FileInputStream inStream = new FileInputStream(file);

String a=readInStream(inStream);

int startPos=a.indexOf("rmnet0:");

a=a.substring(startPos);

Pattern p=pile(" \\d+ ");

Matcher m=p.matcher(a);

while(m.find()){

if(count==8)

{

ReturnLong=Long.parseLong(m.group().trim());

break;

}

count++;

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

return ReturnLong;

}

获取手机Wifi的下载流量//

public static long getWifiRxBytes()

{

long ReturnLong=0; //查询到的结果

try {

File file = new File("/proc/net/dev");

FileInputStream inStream = new FileInputStream(file);

String a=readInStream(inStream);

int startPos=a.indexOf("wlan0:");

a=a.substring(startPos);

Pattern p=pile(" \\d+ ");

Matcher m=p.matcher(a);

while(m.find()){

ReturnLong=Long.parseLong(m.group().trim());

break;

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

return ReturnLong;

}

获取手机Wifi的上传流量//

public static long getWifiTxBytes()

{

long ReturnLong=0; //查询到的结果

try {

int count=0; //返回结果时的计数器

File file = new File("/proc/net/dev");

FileInputStream inStream = new FileInputStream(file);

String a=readInStream(inStream);

int startPos=a.indexOf("wlan0:");

a=a.substring(startPos);

Pattern p=pile(" \\d+ ");

Matcher m=p.matcher(a);

while(m.find()){

if(count==8)

{

ReturnLong=Long.parseLong(m.group().trim());

break;

}

count++;

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

return ReturnLong;

}

根据uid获取进程的下载流量//

public static long getUidRxBytes(int uid)

{

long ReturnLong=0; //查询到的结果

try {

String url="/proc/uid_stat/"+String.valueOf(uid)+"/tcp_rcv";

File file = new File(url);

FileInputStream inStream;

if(file.exists())

{

inStream = new FileInputStream(file);

ReturnLong=Long.parseLong(readInStream(inStream).trim());

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//Log.i(url+"文件并不存在","可能原因为该文件在开机后并没有上过网,所以没有流量记录");

return ReturnLong;

}

根据uid获取进程的上传流量//

public static long getUidTxBytes(int uid)

{

long ReturnLong=0; //查询到的结果

try {

String url="/proc/uid_stat/"+String.valueOf(uid)+"/tcp_snd";

File file = new File(url);

if(file.exists())

{

FileInputStream inStream = new FileInputStream(file);

ReturnLong=Long.parseLong(readInStream(inStream).trim());

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

return ReturnLong;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。