Android2.1读取进程流量

1.大家都知道Android在2.2版本中提供了TrafficStats接口对流量的统计,它提供了一系列的native方法相应统计分类 ,对应的方法如下:

static longgetMobileRxBytes()//获取通过Mobile连接收到的字节总数,不包含WiFi static longgetMobileRxPackets()//获取Mobile连接收到的数据包总数,不包含WiFi static longgetMobileTxBytes()//Mobile发送的总字节数 static longgetMobileTxPackets()//Mobile发送的总数据包数 static longgetTotalRxBytes()//获取总的接受字节数,包含Mobile和WiFi等 static longgetTotalRxPackets()//总的接受数据包数,包含Mobile和WiFi等 static longgetTotalTxBytes()//总的发送字节数,包含Mobile和WiFi等 static longgetTotalTxPackets()//发送的总数据包数,包含Mobile和WiFi等 static longgetUidRxBytes(int uid)//获取某个网络UID的接受字节数 static longgetUidTxBytes(int uid) //获取某个网络UID的发送字节数


2. 但在1.6,2.1呢。。。可以通过对文件的的读取而取得数据流量的数据,所以我猜想TrafficSta应该也通过读文件的方式来取的流量吧。好了不多说了,代码:


public class TrafficService extends Service {

	private Handler handler = new Handler();
	private int intCounter = 0;
	private int mHour;
	private int mMinute;
	private int mYear;
	private int mMonth;
	private int mDay;
	private String mdate;
	final public String DEV_FILE = "/proc/self/net/dev";// 系统流量文件
	String[] ethdata = {
			"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	String[] gprsdata = {
			"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	String[] wifidata = {
			"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
	String data = "0,0,0,0,0,0,0,0,0,0,0,0";// 对应on.txt里面的格式

	final String ETHLINE = "  eth0";// eth是以太网信息 tiwlan0 是 Wifi rmnet0 是 GPRS
	final String GPRSLINE = "rmnet0";
	final String WIFILINE = "tiwlan0";
	final String TEXT_ENCODING = "UTF-8";

	final public String ONPATH = "/data/data/com.example.hello/on.txt";
	final public String LOGPATH = "/data/data/zy.dnh/log.txt";

	private Runnable mTasks = new Runnable() {

		public void run()// 运行该服务执行此函数
		{
			refresh();
			intCounter++;
			// DisplayToast("Counter:"+Integer.toString(intCounter));
			handler.postDelayed(mTasks, 10000);// 每3000毫秒执行一次
		}
	};

	@Override
	public void onStart(Intent intent, int startId) {
		handler.postDelayed(mTasks, 0);
		super.onStart(intent, startId);
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TrafficStatsCompat t= new TrafficStatsCompat();
		// TrafficStats f ;
		return null;
	}

	@Override
	public void onDestroy() {

		/*  */
		handler.removeCallbacks(mTasks);
		super.onDestroy();
	}
	//读设备流量
	public void readdev() {
		FileReader fstream = null;
		try {
			fstream = new FileReader(DEV_FILE);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedReader in = new BufferedReader(fstream, 500);
		String line;
		String[] segs;
		String[] netdata = null;

		int count = 0;
		int k;
		int j;
		try {
			while ((line = in.readLine()) != null) {
				segs = line.trim().split(":");
				if (line.startsWith(ETHLINE)) {

					netdata = segs[1].trim().split(" ");
					for (k = 0, j = 0; k < netdata.length; k++) {
						if (netdata[k].length() > 0) {

							ethdata[j] = netdata[k];
							j++;
						}
					}
				} else if (line.startsWith(GPRSLINE)) {

					netdata = segs[1].trim().split(" ");
					for (k = 0, j = 0; k < netdata.length; k++) {
						if (netdata[k].length() > 0) {

							gprsdata[j] = netdata[k];
							j++;
						}
					}
				} else if (line.startsWith(WIFILINE)) {

					netdata = segs[1].trim().split(" ");
					for (k = 0, j = 0; k < netdata.length; k++) {
						if (netdata[k].length() > 0) {

							wifidata[j] = netdata[k];
							j++;
						}
					}
				}
				count++;
			}
			fstream.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getinfo(String path) {
		File file;
		String str = "";
		FileInputStream in;
		try {
			// 打开文件file的InputStream
			file = new File(path);
			if (!file.exists()) {
				return "";
			}
			in = new FileInputStream(file);
			// 将文件内容全部读入到byte数组
			int length = (int) file.length();
			byte[] temp = new byte[length];
			in.read(temp, 0, length);
			// 将byte数组用UTF-8编码并存入display字符串中
			str = EncodingUtils.getString(temp, TEXT_ENCODING);
			// 关闭文件file的InputStream

			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public void writefile(String str, String path) {
		File file;
		FileOutputStream out;
		try {
			// 创建文件
			file = new File(path);
			file.createNewFile();
			// 打开文件file的OutputStream
			out = new FileOutputStream(file);
			String infoToWrite = str;
			// 将字符串转换成byte数组写入文件
			out.write(infoToWrite.getBytes());
			// 关闭文件file的OutputStream
			out.close();
		} catch (IOException e) {
			// 将出错信息打印到Logcat
		}

	}

	public void refresh() {

		readdev();// 读取本次开机之后直到当前系统的总流量

		data = ethdata[0] + "," + ethdata[1] + "," + ethdata[8] + "," + ethdata[9] + "," + gprsdata[0] + "," + gprsdata[1] + "," + gprsdata[8] + "," + gprsdata[9] + "," + wifidata[0] + ","
				+ wifidata[1] + "," + wifidata[8] + "," + wifidata[9];
		String onstr = getinfo(ONPATH);// 读取on.txt记录到onstr里
		Log.i("sys", "onstr:  " + onstr);

		Log.i("sys", "UID = " + android.os.Process.myUid());

		String ondata[] = onstr.split(",");// 将onstr各项分离 放到ondata里
		int[] delta = new int[12];

		if (ondata.length > 1) {
			delta[0] = Integer.parseInt(ethdata[0]) - Integer.parseInt(ondata[0]);
			delta[1] = Integer.parseInt(ethdata[1]) - Integer.parseInt(ondata[1]);
			delta[2] = Integer.parseInt(ethdata[8]) - Integer.parseInt(ondata[2]);
			delta[3] = Integer.parseInt(ethdata[9]) - Integer.parseInt(ondata[3]);
			delta[4] = Integer.parseInt(gprsdata[0]) - Integer.parseInt(ondata[4]);
			delta[5] = Integer.parseInt(gprsdata[1]) - Integer.parseInt(ondata[5]);
			delta[6] = Integer.parseInt(gprsdata[8]) - Integer.parseInt(ondata[6]);
			delta[7] = Integer.parseInt(gprsdata[9]) - Integer.parseInt(ondata[7]);
			delta[8] = Integer.parseInt(wifidata[0]) - Integer.parseInt(ondata[8]);
			delta[9] = Integer.parseInt(wifidata[1]) - Integer.parseInt(ondata[9]);
			delta[10] = Integer.parseInt(wifidata[8]) - Integer.parseInt(ondata[10]);
			delta[11] = Integer.parseInt(wifidata[9]) - Integer.parseInt(ondata[11]);
		}
		// 计算增量
		else {
			delta[0] = Integer.parseInt(ethdata[0]);
			delta[1] = Integer.parseInt(ethdata[1]);
			delta[2] = Integer.parseInt(ethdata[8]);
			delta[3] = Integer.parseInt(ethdata[9]);
			delta[4] = Integer.parseInt(gprsdata[0]);
			delta[5] = Integer.parseInt(gprsdata[1]);
			delta[6] = Integer.parseInt(gprsdata[8]);
			delta[7] = Integer.parseInt(gprsdata[9]);
			delta[8] = Integer.parseInt(wifidata[0]);
			delta[9] = Integer.parseInt(wifidata[1]);
			delta[10] = Integer.parseInt(wifidata[8]);
			delta[11] = Integer.parseInt(wifidata[9]);
		}

		// 读取log.txt
		// 获取当前时间
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR); // 获取当前年份
		mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
		mDay = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码
		mHour = c.get(Calendar.HOUR_OF_DAY);// 获取当前的小时数
		mMinute = c.get(Calendar.MINUTE);// 获取当前的分钟数
		mdate = mYear + "-" + mMonth + "-" + mDay;

		String logstr = getinfo(LOGPATH);// 将log.txt的内容读到text字符串中
		String[] line = logstr.split("/n");

		Log.i("sys", "logstr:  " + logstr);

		String today = line[line.length - 1];// 获得今日已记录流量
		String[] beToday = today.split(",");
		// 检查文件最后一行是否为今天的流量记录信息
		if (!beToday[0].equals(mdate))// 如果文件只有一行,表明目前日志为空,将当前日期加入
		// 判断今日流量是否已经记录,如果今日流量没有记录
		{

			logstr = logstr + mdate + ",0,0,0,0,0,0,0,0,0,0,0,0/n";
			writefile(logstr, LOGPATH);

			line = logstr.split("/n");
			today = line[line.length - 1];// 获得今日已记录流量

			beToday = today.split(",");
		}

		int i;

		// 处理今日流量
		int[] newTodaydata = new int[12];// 表示今日流量
		String newtoday = mdate;
		for (i = 0; i <= 11; i++) {
			newTodaydata[i] = Integer.parseInt(beToday[i + 1]) + delta[i];
			newtoday = newtoday + "," + newTodaydata[i];
		}
		newtoday = newtoday + "/n";

		String[] beTotal = line[0].split(",");
		int[] newTotaldata = new int[12];// 表示总流量数值
		// 更新第一行
		String newtotal = "total";
		for (i = 0; i <= 11; i++) {
			newTotaldata[i] = Integer.parseInt(beTotal[i + 1]) + delta[i];// 总流量数值+delta[i]更新
			newtotal = newtotal + "," + newTotaldata[i];
		}
		newtotal = newtotal + "/n";
		// 处理中间不变的部分
		String before = "";// before为之前的从第1行到昨天的流量记录

		for (i = 1; i <= line.length - 2; i++)
			before = before + line[i] + "/n";// 代表中间不变的部分

		String newlog = newtotal + before + newtoday;

		Log.i("sys", newlog);

		for (int k = 0; k < delta.length; k++) {
			Log.i("sys", "流量结果:  " + delta[k] + "-------");
		}

		writefile(data, ONPATH);// 更新流量记录
		writefile(newlog, LOGPATH);// 更新log*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值