简体   繁体   English

连接到互联网的Android应用程序列表

[英]List of android applications that connect to internet

I want to implement a listview showing android applications with their internet usage. 我想实现一个列表视图,显示Android应用程序的互联网使用情况。 Fir this, first i have to list all the apps, i have done this using PackageManager, like this: 冷杉,首先我必须列出所有的应用程序,我已经使用PackageManager完成了这个,如下所示:

  packageManager = getPackageManager();
    List<PackageInfo> packageList = packageManager
            .getInstalledPackages(PackageManager.GET_META_DATA);

apkList = (ListView) findViewById(R.id.applist);
    apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));

But this code lists all system apps as well like : Android Sytem, Calculator,Calender, Status Bar, Live Wallpapers etc. which doesnt look appropriate. 但是这段代码列出了所有系统应用程序,如:Android Sytem,计算器,日历,状态栏,动态壁纸等,看起来不合适。 I tried to filter system apps using: 我试图使用以下方法过滤系统应用:

 /*To filter out System apps*/
    for(PackageInfo pi : packageList) {
        boolean b = isSystemPackage(pi);
        if(!b) {
            packageList1.add(pi);
        }
    }

But then the code displays only installed apps, like whatsapp, tango, foursquare etc. It does not show apps like gmail, facebook, browser,maps. 但是代码只显示已安装的应用程序,如whatsapp,tango,foursquare等。它不会显示gmail,facebook,浏览器,地图等应用程序。 Can anybody suggest how should i write the code that only displays list of application that actually use the internet. 任何人都可以建议我应该如何编写仅显示实际使用互联网的应用程序列表的代码。 Thanks in advance! 提前致谢!

I want to implement a listview showing android applications with their internet usage. 我想实现一个列表视图,显示Android应用程序的互联网使用情况。 An anybody suggest how should i write the code that only displays list of application that actually use the internet 任何人都建议我应该如何编写仅显示实际使用互联网的应用程序列表的代码

One solution (maybe only one that works best and came to my head) is to use TrafficStats class that calculating data (TCP, UDP) transferred through network. 一个解决方案(可能只有一个最好用而且来到我的脑海中)是使用TrafficStats类来计算通过网络传输的数据(TCP,UDP)。 Exactly in your case, you need to get data for each UID (each application has own UID). 在您的情况下,您需要获取每个UID的数据(每个应用程序都有自己的UID)。

All what you need to know if application trasfered more that zero bytes through network and when you know that, you can tell that "this application uses network" . 如果应用程序通过网络传输更多零字节,那么您需要知道的所有内容,当您知道这一点时,您可以告诉“此应用程序使用网络”

Here is pseudo-code you could use: 这是您可以使用的伪代码:

List<Application> collection = new ArrayList<Application>();
Application app = null; // some custom object is good approach
PackageManager pm = getActivity().getPackageManager();
for (ApplicationInfo info: pm.getInstalledApplications(
                                                PackageManager.GET_META_DATA)) {

   // received data by application
   long downloaded = TrafficStats.getUidRxBytes(info.uid);

   // transmitted data by application
   long uploaded = TrafficStats.getUidTxBytes(info.uid);

   // filter system applications only
   if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {

      // check if application has network usage
      if (downloaded > 0 || uploaded > 0) {

         // it's application you want
      }
   }
   // non-system application
   else {
      if (downloaded > 0 || uploaded > 0) {

         // it's application you want
      }
   }
}

It's important to say that TrafficStats is available from API 8 and also Before JELLY_BEAN_MR2 , this may return unsupported on devices where statistics aren't available. 重要的是,可以从API 8以及在JELLY_BEAN_MR2之前获得JELLY_BEAN_MR2 ,这可能会返回不支持统计数据不可用的设备。 I used this approach and never had a problems. 我使用这种方法,从来没有遇到过问题。

Note: Also I want to mention that maybe there are another possible approach(es) for example reading from some system files but this is (at least for me) hardcoded approach and i don't recommend to use it (also in various devices files can be on different places, have different content and different filename). 注意:另外我想提一下,也许有另一种可能的方法,例如从一些系统文件读取,但这是(至少对我来说)硬编码方法,我不建议使用它(也在各种设备文件中)可以在不同的地方,有不同的内容和不同的文件名)。

I hope it will help you solve your problem. 我希望它能帮助你解决问题。

应用程序使用互联网将需要互联网权限您可以通过选中的PackageInfo .permission过滤掉这些应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM