Android项目实战_手机安全卫士流量统计

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述Android项目实战_手机安全卫士流量统计相关的知识,希望能为你提供帮助。
【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="https://www.songbingjia.com/android/@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 "";
}
}

1 package com.hb.mobilesafe.activities; 2 3 import com.hb.demo_mobilesafe.R; 4 5 import android.app.Activity; 6 import android.net.TrafficStats; 7 import android.os.Bundle; 8 import android.text.format.Formatter; 9 import android.view.Window; 10 import android.widget.TextView; 11 12 public class FlowStatisticAcitivty extends Activity { 13private TextView tv_wifimobile; 14private TextView tv_mobilestastic; 15@Override 16protected void onCreate(Bundle savedInstanceState) { 17super.onCreate(savedInstanceState); 18requestWindowFeature(Window.FEATURE_NO_TITLE); 19setContentView(R.layout.activity_flowstastic); 20tv_mobilestastic=(TextView) findViewById(R.id.tv_mobilestastic); 21tv_wifimobile=(TextView) findViewById(R.id.tv_wifimobile); 22long totalRxBytes = TrafficStats.getTotalRxBytes(); //下行 23long totalTxBytes = TrafficStats.getTotalTxBytes(); //上行 24 25long mobileRxBytes = TrafficStats.getMobileRxBytes(); 26long mobileTxBytes = TrafficStats.getMobileTxBytes(); 27 28tv_wifimobile.setText(Formatter.formatFileSize(this, totalRxBytes + totalTxBytes)); 29tv_mobilestastic.setText(Formatter.formatFileSize(this, mobileRxBytes + mobileTxBytes)); 30 31 32 33} 34 35 }

 












































































    推荐阅读