zoukankan      html  css  js  c++  java
  • 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 "";
    }
    }

     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 {
    13     private TextView tv_wifimobile;
    14     private TextView tv_mobilestastic;
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         requestWindowFeature(Window.FEATURE_NO_TITLE);
    19         setContentView(R.layout.activity_flowstastic);
    20         tv_mobilestastic=(TextView) findViewById(R.id.tv_mobilestastic);
    21         tv_wifimobile=(TextView) findViewById(R.id.tv_wifimobile);
    22         long totalRxBytes = TrafficStats.getTotalRxBytes();//下行
    23         long totalTxBytes = TrafficStats.getTotalTxBytes();//上行
    24         
    25         long mobileRxBytes = TrafficStats.getMobileRxBytes();
    26         long mobileTxBytes = TrafficStats.getMobileTxBytes();
    27         
    28         tv_wifimobile.setText(Formatter.formatFileSize(this, totalRxBytes + totalTxBytes));
    29         tv_mobilestastic.setText(Formatter.formatFileSize(this, mobileRxBytes + mobileTxBytes));
    30         
    31         
    32         
    33     }
    34 
    35 }
  • 相关阅读:
    比赛:小奔的方案 solution
    比赛:小奔的矩形solution
    比赛:小奔与不等四边形solution
    NOIP2018普及T2暨洛谷P5016 龙虎斗
    Java-GUI基础(三)java.swing
    Java-GUI基础(二)java.awt
    Java-GUI基础(一)
    Java集合(类)框架(三)
    Java集合(类)框架(二)
    Java集合(类)框架(一)
  • 原文地址:https://www.cnblogs.com/boket/p/6991834.html
Copyright © 2011-2022 走看看