Android 获取网络流量信息

package com.zzy.infobackup.model;

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;

/**
 * class name:TrafficStatsLL<BR>
 * class description:获取网络流量信息, 利用读取系统文件的方法来进行获取网络流量<BR>
 * PS:主要意义在于可以应用于2.2以前的没有提供TrafficStats接口的版本 <BR>
 * Date:2013-4-16<BR>
 * 
 * @version 1.0
 * @author wyr
 */
public class TrafficState {
	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的下载流量
	 * 
	 * @return
	 * */
	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的上传流量
	 * 
	 * @return
	 * */
	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的下载流量
	 * 
	 * @return
	 * */
	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的上传流量
	 * 
	 * @return
	 * */
	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获取进程的下载流量
	 * 
	 * @return
	 * */
	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获取进程的上传流量
	 * 
	 * @return
	 * */
	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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值