Android项目实战_手机安全卫士流量统计
## 1.抽屉控件
SlidingDrawer:一定要配置android:handle(把手)和android:content(内容),并在子View中添加把手和内容的布局
```java
<SlidingDrawer
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal" >
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/detail" />
<LinearLayout
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是抽屉里面的内容" />
</LinearLayout>
</SlidingDrawer>
```
## 2.获取总流量
```java
TrafficStats.getMobileRxBytes(); //总的移动网络下载流量
TrafficStats.getMobileTxBytes();//总的移动网络上传流量
TrafficStats.getTotalRxBytes();//总的下载流量
TrafficStats.getTotalTxBytes();//总的网络流量
```
## 3.获取单个应用的流量
```java
PackageManager pm = context.getPackageManager();
List<PackageInfo> packInfos = pm.getInstalledPackages(0);
packageInfo.applicationInfo.uid
TrafficStats.getUidRxBytes(int uid);//某个应用的下载流量
TrafficStats.getUidTxBytes(int uid);//某个应用的上传流量
```
## 4.流量信息保存位置
上传:/proc/uid_stat/[uid]/tcp_snd |udp_snd
下载:/proc/uid_stat/[uid]/tcp_rcv |udp_rcv
## 5.获取文件的数字摘要
```java
/**
* 根据文件路径得到文件的md5算法生成的数字摘要
* @param path
* @return
*/
private String getFileMd5(String path){
try {
File file = new File(path);
//得到一个数字摘要器
MessageDigest digest = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer))!=-1){
digest.update(buffer,0,len);
}
byte[] result = digest.digest();
StringBuilder sb = new StringBuilder();
for(byte b:result){
int number = b&0xff;
String str = Integer.toHexString(number);
if(str.length()==1){
sb.append("0");
}
sb.append(str);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
package com.hb.mobilesafe.activities; import com.hb.demo_mobilesafe.R; import android.app.Activity;
import android.net.TrafficStats;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.Window;
import android.widget.TextView; public class FlowStatisticAcitivty extends Activity {
private TextView tv_wifimobile;
private TextView tv_mobilestastic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_flowstastic);
tv_mobilestastic=(TextView) findViewById(R.id.tv_mobilestastic);
tv_wifimobile=(TextView) findViewById(R.id.tv_wifimobile);
long totalRxBytes = TrafficStats.getTotalRxBytes();//下行
long totalTxBytes = TrafficStats.getTotalTxBytes();//上行 long mobileRxBytes = TrafficStats.getMobileRxBytes();
long mobileTxBytes = TrafficStats.getMobileTxBytes(); tv_wifimobile.setText(Formatter.formatFileSize(this, totalRxBytes + totalTxBytes));
tv_mobilestastic.setText(Formatter.formatFileSize(this, mobileRxBytes + mobileTxBytes)); } }
Android项目实战_手机安全卫士流量统计的更多相关文章
- Android项目实战_手机安全卫士home界面
# 安全卫士主页面# ###1.GridView控件 1.与ListView的使用方式差不多,也要使用数据适配器,通过设置android:numColumns控制显示几列 2.通过指定android: ...
- Android项目实战_手机安全卫士splash界面
- 根据代码的类型组织包结构 1. 界面 com.hb.mobilesafe.activities 2. 服务 com.hb.mobilesafe.services 3. 业务逻辑 com.hb.mo ...
- Android项目实战_手机安全卫士程序锁
###1.两个页面切换的实现1. 可以使用Fragment,调用FragmentTransaction的hide和show方法2. 可以使用两个布局,设置visibility的VISIABLE和INV ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系 context.getSystemService(Context.ACTIVI ...
- Android项目实战--手机卫士开发系列教程
<ignore_js_op> banner131010.jpg (71.4 KB, 下载次数: 0) 下载附件 保存到相册 2 分钟前 上传 Android项目实战--手机卫士01- ...
随机推荐
- HDU 1018 阶乘数的位数
题目大意: 将一个数开阶乘后得到的值,来求这个值的位数 n! = 1*2*3*4...*n 对于求一个数的位数的方法为ans = lg(n!) + 1 那么就可以看作 ans = lg(1) + lg ...
- noip模拟赛 毁灭
题目描述 YJC决定对入侵C国的W国军队发动毁灭性打击.将C国看成一个平面直角坐标系,W国一共有n^2个人进入了C国境内,在每一个(x,y)(1≤x,y≤n)上都有恰好一个W国人.YJC决定使用m颗核 ...
- resin4开启jmx
https://blog.csdn.net/liuxiao723846/article/details/51321010 https://blog.csdn.net/u010342008/articl ...
- [bzoj1617][Usaco2008 Mar]River Crossing渡河问题_动态规划
River Crossing渡河问题 bzoj-1617 Usaco-2008 Mar 题目大意:题目链接. 注释:略. 想法:zcs0724出考试题的时候并没有发现这题我做过... 先把m求前缀和, ...
- Ubuntu 16.04添加阿里云源/163源
添加国内源有个好处,比如下载软件时直接时国内的服务器,速度有保证. 以下是操作方法: 1.备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list. ...
- CHAPTER 1 Architectural Overview of Oracle Database 11g
Which SGA structures are required, and which are optional? The database buffer cache, log buffer, an ...
- Azure Event hub usage
1. create event hub on azure watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/40 ...
- css list menu
选择让page和folder都显示出来
- 【BZOJ 2457】 双端队列
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2457 [算法] 贪心 [代码] #include<bits/stdc++.h& ...
- 【HDU 1007】 Quoit Design
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1007 [算法] 答案为平面最近点对距离除以2 [代码] #include <algorith ...