user2011302
user2011302

Reputation: 391

List of android applications that connect to internet

I want to implement a listview showing android applications with their internet usage. Fir this, first i have to list all the apps, i have done this using PackageManager, like this:

  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. 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. Can anybody suggest how should i write the code that only displays list of application that actually use the internet. Thanks in advance!

Upvotes: 4

Views: 383

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33505

I want to implement a listview showing android applications with their internet usage. 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. Exactly in your case, you need to get data for each UID (each application has own 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. 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.

Upvotes: 4

justHooman
justHooman

Reputation: 3054

Application use internet will need Internet Permission You can filter out those app by checked PackageInfo.permission

Upvotes: 0

Related Questions