关于2.2版本之前的流量统计
2012-09-28 15:06
197 查看
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 2011-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=Pattern.compile(" \\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=Pattern.compile(" \\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=Pattern.compile(" \\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=Pattern.compile(" \\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;
}
}
本帖地址: http://www.eoeandroid.com/thread-201729-1-1.html
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 2011-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=Pattern.compile(" \\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=Pattern.compile(" \\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=Pattern.compile(" \\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=Pattern.compile(" \\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;
}
}
本帖地址: http://www.eoeandroid.com/thread-201729-1-1.html
相关文章推荐
- android 关于2.2版本之前的流量统计
- unity 中关于鼠标悬停 不同于之前写的文章版本 这个更加实用
- 关于Android:lineSpacingExtra属性在Android5.0与之前版本存在的区别
- Swift基础之UITableView(之前写的知识点都是最新的2.2版本样式,欢迎大家参考,可以相互交流)
- Swift基础之UITableView(之前写的知识点都是最新的2.2版本样式,欢迎大家参考,可以相互交流)
- 关于cocos2d-x 2.2 版本创建工程问题
- 关于iReport-5.6.0之前版本 “无法启动”或者“一开就关闭”的 某些原因
- 关于转屏禁止onCreate android 3.0版本之前和之后的区别
- 关于android:lineSpacingExtra属性 在android5.0与之前版本存在的区别
- 关于iReport-5.6.0之前版本 “无法启动”或者“一开就关闭”的 某些原因
- 关于Android 2.2与2.3 adb.exe版本更新问题(在安装android——SDK时候找不到相应的 /tools/adb.exe! )
- 关于iReport-5.6.0之前版本 “无法启动”或者“一开就关闭”的 某些原因
- 关于Android Studio2.2版本logcat日志闪退显示不全的解决办法
- Windows下的Android SDK下载,2.2之前各个版本及Google API,文档说明等
- Windows下的Android SDK下载,2.2之前各个版本及Google API,文档说明等
- 关于Android 2.2与2.3 adb.exe版本更新问题(在安装android——SDK时候找不到相应的 /tools/adb.exe! )
- BUG:InstallShield 2012 Spring之前版本的帮助文档中关于StrGetTokens示例代码的错误
- 关于如何查看mysql版本及其端口号
- 关于程序的Debug和Release版本的注意
- iOS6兼容之前版本的旋屏总结