Android--获取使用的总流量和每个App的上传、下载的流量

时间:2024-03-10 20:39:58

获得每个App的上传、下载的流量。

思路就是获取到我们手机上的所有app,再获得app里面使用的权限,如果app有网络权限,就显示出来。

代码很简单,代码里面也有比较详细的注释,下面直接上代码

布局文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     android:paddingBottom="@dimen/activity_vertical_margin"
10     tools:context="com.example.fanlei.mycontentproviderdemo.TrafficActivity">
11 
12     <TextView
13         android:id="@+id/show_liuliang"
14         android:text="@string/hello_world"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content" />
17 
18     <Button
19         android:id="@+id/btn_scan"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:text="获取App流量"/>
23 
24     <ListView
25         android:id="@+id/listView"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content">
28 
29     </ListView>
30 </LinearLayout>

主函数:

  1 package com.example.fanlei.mycontentproviderdemo;
  2 
  3 import android.content.pm.PackageInfo;
  4 import android.content.pm.PackageManager;
  5 import android.net.TrafficStats;
  6 import android.os.Bundle;
  7 import android.support.v7.app.ActionBarActivity;
  8 import android.view.View;
  9 import android.widget.Button;
 10 import android.widget.ListView;
 11 import android.widget.SimpleAdapter;
 12 import android.widget.TextView;
 13 
 14 import java.util.ArrayList;
 15 import java.util.HashMap;
 16 import java.util.List;
 17 import java.util.Map;
 18 
 19 
 20 public class TrafficActivity extends ActionBarActivity {
 21 
 22     private TextView show_liuliang;//显示流量
 23     private Button   btn_scan;
 24     private ListView listView;
 25 
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_traffic_activity);
 30 
 31         show_liuliang = (TextView) findViewById(R.id.show_liuliang);
 32         listView      = (ListView) findViewById(R.id.listView);
 33         btn_scan      = (Button) findViewById(R.id.btn_scan);
 34 
 35         long mrb = TrafficStats.getMobileRxBytes();   //手机接收的字节数,非WiFi状态
 36 
 37         long mtb = TrafficStats.getMobileTxBytes();   //手机发送的字节数,非WiFi状态
 38 
 39         long tMrb = TrafficStats.getTotalRxBytes();   //全部接收的字节数
 40 
 41         long tMtb = TrafficStats.getTotalTxBytes();   //全部发送的字节数
 42 
 43         StringBuilder sb = new StringBuilder();
 44         sb.append("非WiFi总接受:").append(byteToMB(mrb));
 45         sb.append("非WiFi总发送:").append(byteToMB(mtb));
 46         sb.append("全部接收:").append(byteToMB(tMrb));
 47         sb.append("全部发送:").append(byteToMB(tMtb));
 48 
 49         show_liuliang.setText(sb.toString());//显示
 50 
 51         btn_scan.setOnClickListener(new View.OnClickListener() {
 52             @Override
 53             public void onClick(View v) {
 54                 PackageManager pm = getPackageManager();             //拿到包管理者
 55                 //拿到包的信息  PackageInfo是系统的一个类
 56                 List<PackageInfo> info = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES
 57                                                                  |PackageManager.GET_PERMISSIONS);
 58                 List<Map<String,String>> appInfo = new ArrayList<>();//用于存放App的名称,上传和下载的字节
 59                 /**
 60                  * 通过看它有没有网络权限权限
 61                  */
 62                 for (PackageInfo temp:info){
 63                     String permissions[] = temp.requestedPermissions;    //拿到该包的权限
 64                     if (permissions!= null && permissions.length > 0){   //如果有权限
 65                         for (String p : permissions){
 66                             if (p.equals("android.permission.INTERNET")){//是否有网络权限
 67                                 String appName = temp.applicationInfo.loadLabel(pm).toString();       //App名称
 68                                 long appMrb    = TrafficStats.getUidRxBytes(temp.applicationInfo.uid);//App接收的字节
 69                                 long appMtb    = TrafficStats.getUidTxBytes(temp.applicationInfo.uid);//App发送的字节
 70                                 String str = appName + "下载:"+byteToMB(appMrb)+"上传:"+byteToMB(appMtb);
 71                                 Map<String,String> map = new HashMap<>();
 72                                 map.put("info",str);
 73                                 appInfo.add(map);
 74                             }
 75                         }
 76                     }
 77                 }
 78 
 79                 SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(),
 80                         appInfo,
 81                         android.R.layout.simple_list_item_1,
 82                         new String[]{"info"},
 83                         new int[]{android.R.id.text1});
 84 
 85                 listView.setAdapter(simpleAdapter);//设置Adapter
 86             }
 87         });
 88     }
 89     //将字节数转化为MB
 90     private String byteToMB(long size){
 91         long kb = 1024;
 92         long mb = kb*1024;
 93         long gb = mb*1024;
 94         if (size >= gb){
 95             return String.format("%.1f GB",(float)size/gb);
 96         }else if (size >= mb){
 97             float f = (float) size/mb;
 98             return String.format(f > 100 ?"%.0f MB":"%.1f MB",f);
 99         }else if (size > kb){
100             float f = (float) size / kb;
101             return String.format(f>100?"%.0f KB":"%.1f KB",f);
102         }else {
103             return String.format("%d B",size);
104         }
105     }
106 
107 }

虚拟机效果图,由于我的虚拟机不能上网,故没有流量的使用