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)); } }
最新文章
- 关于mysql数据库的备份和还原
- inno setup读取注册表遇到的一个坑
- js倒计时跳转页面
- Git的常用操作,记录下
- get( )与getline( )区别
- 【linux】grub加密
- php的webservice的soapheader认证问题
- android 布局居中
- 制作下拉菜单(PopupList)
- Unity3d之Socket UDP协议
- 调用test case集,并生成测试报告
- 高性能web系统的架构和系统优化
- JS中setAttribute的兼容性问题(摘自leejersey)
- 用Owin Host实现脱离IIS跑Web API单元测试
- 协议端口号(protocol port number)
- Java常用异常整理
- bzoj 2565: 最长双回文串
- wepy 初探
- [Educational Round 5][Codeforces 616F. Expensive Strings]
- 通过P3P头实现跨域设置cookie
热门文章
- jQuery通过event获取点击事件的事件对象
- Hihocoder 1325 (splay)
- NOIP2010 提高组合集
- Screenshot: available via screen
- [转] python 获取本机ip地址的两种实现方法
- vijos 1237 隐形的翅膀
- Android: 长按及松开处理
- C++对象内存分布(3) - 菱形继承(virtual)
- Codeforces 196 D. The Next Good String
- R语言学习笔记-Error in ts(x):对象不是矩阵问题解决