How do I programmatically show data usage of all applications?

后端 未结 4 737
温柔的废话
温柔的废话 2020-12-13 10:37

On Android 4.0 onwards we have data usage control options in the phone. Please check the attached screen shot for further understanding.

http://developer.android.com

4条回答
  •  盖世英雄少女心
    2020-12-13 11:33

    The solution from Arunendra, dated 2015, didn't immediately work for me on SDK 28 (Pie).

    So I modified as follows:

    void networkUsage() {
        // Get running processes
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List runningApps = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo runningApp : runningApps) {
            long received = TrafficStats.getUidRxBytes(runningApp.uid);
            long sent = TrafficStats.getUidTxBytes(runningApp.uid);
            Log.d(LOG_TAG, String.format(Locale.getDefault(), 
                    "uid: %1d - name: %s: Sent = %1d, Rcvd = %1d", runningApp.uid, runningApp.processName, sent, received));
        }
    }
    

提交回复
热议问题