首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 2.1 获取手机流量工具种

2012-09-21 
android 2.1 获取手机流量工具类前面一篇博文【点我】说道,andorid2.1没有提供取得流量的api,所以自己写了个

android 2.1 获取手机流量工具类
前面一篇博文【点我】说道,andorid2.1没有提供取得流量的api,所以自己写了个……

/** *  */package com.born.test.dev.util;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;/** *  * @author 许力多 *  */public class MTrafficStats {private static final String TAG = "MTrafficStats";public final static String DEV_FILE = "/proc/self/net/dev";// 系统流量文件public final static String ETHLINE = "eth0";// eth是以太网信息public final static String GPRSLINE = "rmnet0"; // rmnet0 是 GPRSpublic final static String WIFILINE = "tiwlan0"; // tiwlan0 是 Wifiprivate static String[][] traffic = { { "0", "0" }, { "0", "0" } };// [0][*]GPRS// [1][*]wifi// [*][0]Receive// [*][1]Transmitprivate static File srcfile;// private static RandomAccessFile file;// private long mobileRxBytes = 0;// private long mobileTxBytes = 0;/*** * The return value to indicate that the device does not support the * statistic. */public final static int UNSUPPORTED = -1;/** * 设置 流量 *  * @param type *            0、GPRS 1、wifi */private static void setTraffic(int type, String rx, String tx) {if (type > 1) {return;}traffic[type][0] = rx;traffic[type][1] = tx;}/** * 设置流量对象, 换成nio效率要高点,不过这个文件非常小,读取只有0.x毫秒 */private synchronized static void setTraffic() {if (srcfile == null) {srcfile = new File(DEV_FILE);}FileReader fstream = null;BufferedReader in = null;try {fstream = new FileReader(srcfile);in = new BufferedReader(fstream, 128);String line;String[] segs;while ((line = in.readLine()) != null) {segs = line.trim().split(":");if (line.startsWith(GPRSLINE)) {String temp[] = segs[1].trim().split("\\t+|\\s+");if (temp.length > 8) {setTraffic(0, temp[0], temp[8]);}} else if (line.startsWith(WIFILINE)) {String temp[] = segs[1].trim().split("\\t+|\\s+");if (temp.length > 8) {setTraffic(1, temp[0], temp[8]);}}}} catch (FileNotFoundException e) {MLog.e(TAG, "getMobileTxBytes", e);} catch (IOException e) {MLog.e(TAG, "getMobileTxBytes", e);} finally {if (fstream != null) {try {fstream.close();} catch (IOException e) {}}if (in != null) {try {in.close();} catch (IOException e) {}}}}/** * 未实现 *  * @return */public static long getMobileTxPackets() {return UNSUPPORTED;}/** * 未实现 *  * @return */public static long getMobileRxPackets() {return UNSUPPORTED;}/** * 获得及时上传流量(mobile) rmnet0 's Transmit *  * @return */public static long getMobileTxBytes() {setTraffic();try {return Long.parseLong(traffic[0][1]);} catch (Exception e) {MLog.e(TAG, "error-->" + traffic[0][1]);MLog.e(TAG, "Exception", e);return UNSUPPORTED;}}/** * 获得及时下载流量(mobile) rmnet0 's Receive *  * @return */public static long getMobileRxBytes() {setTraffic();try {return Long.parseLong(traffic[0][0]);} catch (Exception e) {MLog.e(TAG, "error-->" + traffic[0][0]);MLog.e(TAG, "Exception", e);return UNSUPPORTED;}}/** * 未实现 *  * @return -1 */public static long getTotalTxPackets() {return UNSUPPORTED;}/** * 未实现 *  * @return -1 */public static long getTotalRxPackets() {return UNSUPPORTED;}/** * 获得总的上传流量(mobile + wifi) rmnet0 & Transmit 's Transmit *  * @return */public static long getTotalTxBytes() {setTraffic();try {return Long.parseLong(traffic[0][1])+ Long.parseLong(traffic[1][1]);} catch (Exception e) {MLog.e(TAG, "error-->" + traffic[0][1] + ":" + traffic[1][1]);MLog.e(TAG, "Exception", e);return UNSUPPORTED;}}/** * 获得总的下载流量(mobile + wifi) rmnet0 & Transmit 's Receive *  * @return */public static long getTotalRxBytes() {setTraffic();try {return Long.parseLong(traffic[0][0])+ Long.parseLong(traffic[1][0]);} catch (Exception e) {MLog.e(TAG, "error-->" + traffic[0][1] + ":" + traffic[1][0]);MLog.e(TAG, "Exception", e);return UNSUPPORTED;}}/** * 未实现 *  * @param uid * @return */public static long getUidTxBytes(int uid) {return UNSUPPORTED;}/** * 未实现 *  * @param uid * @return */public static long getUidRxBytes(int uid) {return UNSUPPORTED;}}


PS:MLog是偶自定义的日志封装类,可以直接替换成Log就是了

热点排行