Nouman Shah Nouman Shah - 7 months ago 12
Java Question

Can I access internet data used by user from Internet data Usage

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 ?

Answer

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;}