Android获取当前系统正在运行应用及其数据流量

PackageManager pm = getPackageManager();
ApplicationInfo info =null;
PackageInfo pInfo = null;
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> run  = am.getRunningAppProcesses();
for (RunningAppProcessInfo ra : run) {
   try {
       info = pm.getApplicationInfo(ra.processName, PackageManager.GET_META_DATA);
       //pInfo = pm.getPackageInfo(ra.processName, PackageManager.GET_ACTIVITIES);
    } catch (NameNotFoundException e) {
         Log.e("DisableWifiService", ra.processName);
         //e.printStackTrace();
    }
       Long receiver = TrafficStats.getUidRxBytes(ra.uid);//接收的字节数
       Long sent = TrafficStats.getUidTxBytes(ra.uid) ; //发送的字节数
       System.out.println(ra.uid +": "+pm.getApplicationLabel(info)+": " + receiver +":" + sent);
 
}

注意该方法是统计手机从开机的总流量,用户关机后再重启后流量从0开始!

你可能感兴趣的:(Android获取当前系统正在运行应用及其数据流量)