当前位置:操作系统 > 安卓/Android >>

android 流量监控实现想法

经过2,3周的时间研究流量的监控,什么TrafficStats提供的方法,做的时候蛋疼的要易做图,2G/3G的流量获取根本就不准确,至于什么原因网络上面也有说,就不讲了。经过研究,通过读取/proc/uid_stat/项目uid/下面的两个文件,获取流量信息。再通过自己的逻辑算法,解决了流量监控和统计的问题。可能也有不准确的地方,请多多指导
 
本监控软件,实现的是监控一个月的程序使用流量
 
1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量
        每一个小时,更新一次流量,用于清除非本月的流量
 
2,保存流量的时候,进行判断
        a,若是数据库中保存的   旧流量oldFlow > 文件流量newFlow,说明手机重启过, 保存流量 = 前一次数据库保存流量 + 文件流量
        b,若是数据库中保存的   旧流量oldFlow <= 文件流量newFlow,说明软件消耗了新流量,保存流量=oldFlow-newFlow+前一次数据库保存流量
 
3,晚上24点的处理
        文件流量不为o的时候,当前一天oldFLow > newFlow,保存流量 = newFlow + 0; 此时oldFlow = newFLow
                                             当前一天oldFlow <= newFlow, 保存流量 = newFlow - oldFlow +0; 此时oldFlow = newFlow;
 
 
/**
 * 关键代码
 */
FlowInfo uidInfo;
 
public List<FlowInfo> getGPRSData() {
 
List<FlowInfo> uidList = new ArrayList<FlowInfo>();
PackageManager pm = context.getPackageManager();
List<PackageInfo> packinfos = pm
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES
| PackageManager.GET_PERMISSIONS);
 
long newFlow = 0;
long[] oldStandardFlow = null;
 
String date = TimeUtil.getInstance().getStringDate();
 
for (PackageInfo info : packinfos) {
uidInfo = new FlowInfo();
String[] premissions = info.requestedPermissions;
if (premissions != null && premissions.length > 0) {
for (String premission : premissions) {
if ("android.permission.INTERNET".equals(premission)) {
int uid = info.applicationInfo.uid;
uidInfo.setUid(uid);
newFlow = Long.valueOf(getSndFlowByUid(uid))
+ Long.valueOf(getRcvFlowByUid(uid));
tableHelp = TableHelp.getInstance(context,
"FlowMonitors.db", 1);
sqlite = tableHelp.getSqliteDatabase();
oldStandardFlow = tableHelp.getOldStandardFlow(sqlite,
uid, date); // 保存的旧流量和标准流量
if (oldStandardFlow[0] == 0 && oldStandardFlow[1] == 0
&& newFlow != 0) { // 晚上24点后的处理
int d = Integer.valueOf(TimeUtil.getInstance()
.getStringDate1());
String d2;
if (d < 10) {
d2 = TimeUtil.getInstance().getYearAndMonth()
+ "-0" + (d - 1);
} else {
d2 = TimeUtil.getInstance().getYearAndMonth()
+ "-" + (d - 1);
}
sqlite = tableHelp.getSqliteDatabase();
oldStandardFlow = tableHelp.getOldStandardFlow(
sqlite, uid, d2); // 保存的旧流量和标准流量
setFlowAfter24(oldStandardFlow, newFlow);
} else { // 当天处理
setFlowToday(oldStandardFlow, newFlow);
}
uidInfo.setDate(date);
uidList.add(uidInfo);
}
}
}
}
return uidList;
}
 
public void setFlowToday(long[] oldStandardFlow, long newFlow) {
if (oldStandardFlow[0] > newFlow) {
uidInfo.setOldFlow(newFlow);
newFlow = oldStandardFlow[1] + newFlow;
uidInfo.setFlow(newFlow);
} else { 
uidInfo.setOldFlow(newFlow);
newFlow = newFlow - oldStandardFlow[0] + oldStandardFlow[1];
uidInfo.setFlow(newFlow);
}
}
public void setFlowAfter24(long[] oldStandardFlow, long newFlow) {
if (oldStandardFlow[0] > newFlow) { 
uidInfo.setOldFlow(newFlow);
newFlow = 0 + newFlow;
uidInfo.setFlow(newFlow);
} else {
uidInfo.setOldFlow(newFlow);
newFlow = newFlow - oldStandardFlow[0] + 0;
uidInfo.setFlow(newFlow);
}
}
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,