1

TrafficStats class provides total data since boot can I directly access internet data usage from android

e.g Call history can be directly fetched from call log. So is it possible to get internet data from internet data usage.

And how to get internet data usage on the basic of application ?

Nouman Shah
  • 534
  • 1
  • 9
  • 22
  • 1
    Possible duplicate of [Android: How can I find the Data Usage on a Per-Application Basis?](http://stackoverflow.com/questions/11939939/android-how-can-i-find-the-data-usage-on-a-per-application-basis) – ΦXocę 웃 Пepeúpa ツ Feb 06 '16 at 09:21
  • actually TrafficState class provide Real time data i want to get past month data can i get it as i get call record from CallLog – Nouman Shah Feb 09 '16 at 20:11

1 Answers1

4

so for I know we cannot directly get internet data usage from android as we fetch call history record form call log we can get data form TrafficStats class which is since boot

To get internet data on application basic here is the simple code using packageManager to get package name of the corresponding app and using ApplicationInfo we can find the detail of the application information

 final PackageManager pm = context.getPackageManager();
        // get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            // get the UID for the selected app
            UID = packageInfo.uid;
            String package_name = packageInfo.packageName;
          Log.d("mypackagename",package_name+"");
            ApplicationInfo app = null;
            try {
                app = pm.getApplicationInfo(package_name, 0);
            } catch (PackageManager.NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String name = (String) pm.getApplicationLabel(app);
            Drawable icon = pm.getApplicationIcon(app);
            // internet usage for particular app(sent and received)
            double received = (double) TrafficStats.getUidRxBytes(UID)
                    / (1024 * 1024);
            double send = (double) TrafficStats.getUidTxBytes(UID)
                    / (1024 * 1024);
            double totalab = received + send;}
Nouman Shah
  • 534
  • 1
  • 9
  • 22