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- ...
随机推荐
- Codeforces Round #235 (Div. 2)
A. Vanya and Cards time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- [K/3Cloud] KSQL 关联表更新字段Update语法
关联表更新字段 UPDATE tmp369faa3f7d224b0595670425008 as t1 SET FStatus=-1 where exists(select 1 from t_BD_S ...
- android中listview点击事件的监听实现
listview_bookmark.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public vo ...
- Android: 清除View跳转的历史记录
Intent intent = new Intent(); intent.setClass(SetActivity.this, RegisterLoginActivity.class); intent ...
- ZOJ 题目2859 Matrix Searching(二维RMQ)
Matrix Searching Time Limit: 10 Seconds Memory Limit: 32768 KB Given an n*n matrix A, whose ent ...
- docker Cannot start container [8] System error: exec format error
docker Cannot start container [8] System error: exec format error 学习了:https://www.aliyun.com/jiaoch ...
- DB9针型:RS485输出信号及接线端子引脚分配
下图所看到的.DB9针型RS485输出信号及接线端子引脚分配. 此DB9针型与 标准 RS232 or RS485 DB9定义有所不同,下图中的DB9针型说明仅是针对USB转485DB9接口. wat ...
- UIView加入手势 然后UITableView 加入进这个View 导致UITableView 的单元格点击事件无效
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableVi ...
- U4687 不无聊的序列
U4687 不无聊的序列 0通过 85提交 题目提供者飞翔 标签 难度尚无评定 提交 最新讨论 暂时没有讨论 题目背景 如果一个序列的任意一个连续的子序列中没有只出现一次的元素,辣么kkk就认为这个序 ...
- java的PrintStream(打印输出流)详解(java_io)
java的PrintStream(打印输出流)详解(java_io) 本章介绍PrintStream以及 它与DataOutputStream的区别.我们先对PrintStream有个大致认识,然后再 ...