手机总流量统计
发布日期:2021-06-30 18:39:34 浏览次数:3 分类:技术文章

本文共 1864 字,大约阅读时间需要 6 分钟。

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import android.content.Context;import android.text.format.Formatter;/** * 流量统计的业务类 * @author Administrator * */public class ConnectivityEngine {
/** * @return * 接收的流量信息(格式化的如:33MB) */ public static String getReceive(int uid,Context context){ String res = null; //读取流量信息文件 /proc/uid_stat/uid/tcp_rcv String path = "/proc/uid_stat/" + uid + "/tcp_rcv"; try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String line = reader.readLine(); long size = Long.parseLong(line); res = Formatter.formatFileSize(context, size); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } /** * @return * 发送的流量信息(格式化的如:33MB) */ public static String getSend(int uid,Context context){ String res = null; //读取流量信息文件 /proc/uid_stat/uid/tcp_snd String path = "/proc/uid_stat/" + uid + "/tcp_snd"; try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String line = reader.readLine(); long size = Long.parseLong(line); res = Formatter.formatFileSize(context, size); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; }}

转载地址:https://linqiarui.blog.csdn.net/article/details/51173875 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:集成病毒数据查询
下一篇:Home键监听

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月10日 23时45分24秒