日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 关于2.2版本之前的流量统计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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


2.2以前流量統(tǒng)計可以使用
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 {
/**
* 獲取網絡流量信息
* 利用讀取系統(tǒng)文件的方法來進行獲取網絡流量
* 主要意義在于可以應用于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;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回結果時的計數(shù)器
? ? ? ? ? ? ? ? ? ? ? ? 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;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回結果時的計數(shù)器
? ? ? ? ? ? ? ? ? ? ? ? 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;
? ? ? ? }
? ? ? ??
根據(jù)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;
? ? ? ? }


根據(jù)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;
? ? ? ? }
}

總結

以上是生活随笔為你收集整理的android 关于2.2版本之前的流量统计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。