Android 获取手机整体流量使用情况以及某个应用的流量的统计

编程入门 行业动态 更新时间:2024-10-28 08:19:36

Android 获取手机整体<a href=https://www.elefans.com/category/jswz/34/1770754.html style=流量使用情况以及某个应用的流量的统计"/>

Android 获取手机整体流量使用情况以及某个应用的流量的统计

很多安全卫士类软件都实现了网速监测功能,也算是一个比较实用的功能。Android下,TrafficStats类实现了对流量的统计。

/proc/uid_stat/uid/tcp_send        上传流量
/proc/uid_stat/uid/tcp_rcv         下载流量

static long getMobileRxBytes()//获取通过Mobile连接收到的字节总数,但不包含WiFi   
static long getMobileRxPackets()//获取Mobile连接收到的数据包总数   
static long getMobileTxBytes()//Mobile发送的总字节数   
static long getMobileTxPackets()//Mobile发送的总数据包数   
static long getTotalRxBytes()//获取总的接受字节数,包含Mobile和WiFi等   
static long getTotalRxPackets()//总的接受数据包数,包含Mobile和WiFi等   
static long getTotalTxBytes()//总的发送字节数,包含Mobile和WiFi等   
static long getTotalTxPackets()//发送的总数据包数,包含Mobile和WiFi等   
static long getUidRxBytes(int uid)//获取某个网络UID的接受字节数   
static long getUidTxBytes(intuid) //获取某个网络UID的发送字节数  


获取某个应用的流量的统计(根据包名)

一、根据包名获取相应的UID;

/*** 获取当前应用uid* @return*/public int getUid() {try {PackageManager pm = mContext.getPackageManager();ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);return ai.uid;} catch (NameNotFoundException e) {e.printStackTrace();}return -1;}

二、根据UID获取相应的流量的统计

TrafficInfo tr=new TrafficInfo(getApplicationContext());int uid=tr.getUid();Log.e("asdf",uid+"");Log.e("asdf",tr.getRcvTraffic()+"--"+tr.getSndTraffic()+"-----"+tr.getTrafficInfo());


关键的类:

package com.rzt.qualitytest.utils;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.TrafficStats;
import android.os.Handler;
import android.os.Message;
import android.util.Log;/*** 应用的流量信息*/
public class TrafficInfo {private static final int UNSUPPORTED = -1;private static final String LOG_TAG = "test";private static TrafficInfo instance;static int uid=0;private long preRxBytes = 0;private Timer mTimer = null;/** 更新频率(每几秒更新一次,至少1秒) */private final int UPDATE_FREQUENCY = 1;private int times = 1;private Context mContext;public TrafficInfo(Context mContext, int uid) {this.uid = uid;this.mContext=mContext;}public TrafficInfo(Context mContext) {this.mContext=mContext;}/*** 获取总流量* * @return*/public long getTrafficInfo() {long rcvTraffic = UNSUPPORTED; // 下载流量long sndTraffic = UNSUPPORTED; // 上传流量rcvTraffic = getRcvTraffic();sndTraffic = getSndTraffic();if (rcvTraffic == UNSUPPORTED || sndTraffic == UNSUPPORTED)return UNSUPPORTED;elsereturn rcvTraffic + sndTraffic;}/*** 获取下载流量 某个应用的网络流量数据保存在系统的/proc/uid_stat/$UID/tcp_rcv | tcp_snd文件中* * @return*/public long getRcvTraffic() {long rcvTraffic = UNSUPPORTED; // 下载流量if(uid==0){uid=getUid();}rcvTraffic = TrafficStats.getUidRxBytes(uid);if (rcvTraffic == UNSUPPORTED) { // 不支持的查询return UNSUPPORTED;}Log.i("test", rcvTraffic + "--1");RandomAccessFile rafRcv = null, rafSnd = null; // 用于访问数据记录文件String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv";try {rafRcv = new RandomAccessFile(rcvPath, "r");rcvTraffic = Long.parseLong(rafRcv.readLine()); // 读取流量统计} catch (FileNotFoundException e) {Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());rcvTraffic = UNSUPPORTED;} catch (IOException e) {Log.e(LOG_TAG, "IOException: " + e.getMessage());e.printStackTrace();} finally {try {if (rafRcv != null)rafRcv.close();if (rafSnd != null)rafSnd.close();} catch (IOException e) {Log.w(LOG_TAG, "Close RandomAccessFile exception: " + e.getMessage());}}Log.i("test", rcvTraffic + "--2");return rcvTraffic;}/*** 获取上传流量* * @return*/public long getSndTraffic() {long sndTraffic = UNSUPPORTED; // 上传流量if(uid==0){uid=getUid();}sndTraffic = TrafficStats.getUidTxBytes(uid);if (sndTraffic == UNSUPPORTED) { // 不支持的查询return UNSUPPORTED;}RandomAccessFile rafRcv = null, rafSnd = null; // 用于访问数据记录文件String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd";try {rafSnd = new RandomAccessFile(sndPath, "r");sndTraffic = Long.parseLong(rafSnd.readLine());} catch (FileNotFoundException e) {Log.e(LOG_TAG, "FileNotFoundException: " + e.getMessage());sndTraffic = UNSUPPORTED;} catch (IOException e) {Log.e(LOG_TAG, "IOException: " + e.getMessage());e.printStackTrace();} finally {try {if (rafRcv != null)rafRcv.close();if (rafSnd != null)rafSnd.close();} catch (IOException e) {Log.w(LOG_TAG, "Close RandomAccessFile exception: " + e.getMessage());}}return sndTraffic;}/*** 获取当前下载流量总和* * @return*/public static long getNetworkRxBytes() {return TrafficStats.getTotalRxBytes();}/*** 获取当前上传流量总和* * @return*/public static long getNetworkTxBytes() {return TrafficStats.getTotalTxBytes();}/*** 获取当前网速* * @return*/public double getNetSpeed() {long curRxBytes = getNetworkRxBytes();if (preRxBytes == 0)preRxBytes = curRxBytes;long bytes = curRxBytes - preRxBytes;preRxBytes = curRxBytes;//int kb = (int) Math.floor(bytes / 1024 + 0.5);double kb = (double)bytes / (double)1024;BigDecimal bd = new BigDecimal(kb);return bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();}public void stopCalculateNetSpeed() {if (mTimer != null) {mTimer.cancel();mTimer = null;}}/*** 获取当前应用uid* @return*/public int getUid() {try {PackageManager pm = mContext.getPackageManager();ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);return ai.uid;} catch (NameNotFoundException e) {e.printStackTrace();}return -1;}/*** static long getMobileRxBytes()//获取通过Mobile连接收到的字节总数,但不包含WiFi static long* getMobileRxPackets()//获取Mobile连接收到的数据包总数 static long* getMobileTxBytes()//Mobile发送的总字节数 static long* getMobileTxPackets()//Mobile发送的总数据包数 static long* getTotalRxBytes()//获取总的接受字节数,包含Mobile和WiFi等 static long* getTotalRxPackets()//总的接受数据包数,包含Mobile和WiFi等 static long* getTotalTxBytes()//总的发送字节数,包含Mobile和WiFi等 static long* getTotalTxPackets()//发送的总数据包数,包含Mobile和WiFi等 static long* getUidRxBytes(int uid)//获取某个网络UID的接受字节数 static long getUidTxBytes(int* uid) //获取某个网络UID的发送字节数*/}
转载:

更多推荐

Android 获取手机整体流量使用情况以及某个应用的流量的统计

本文发布于:2024-02-25 20:53:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流量   情况   手机   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!