How do I programmatically show data usage of all applications?

Deadly 提交于 2019-12-17 22:16:25

问题


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/about/versions/android-4.0-highlights.html

Now I have some requirement to check these things (All Application's Data usage in specific time period/specific days) from my application. How can I achieve this? I am also using the below class for Network Usage details.

http://developer.oesf.biz/em/developer/reference/eggplant/android/net/NetworkStatsHistory.html

Please check the below link images. I need to develop the same kind of application.

  • http://developer.android.com/sdk/images/4.0/usage-all-lg.png

  • http://developer.android.com/sdk/images/4.0/usage-maps-lg.png

Thanks for sharing your code, but I need to know data used by each application instead of all applications. So far I observed in the links no one is talking about data usage of individual applications. I already know how to show installed applications in the device. Now I would like to know what's the data used by each and every application.

I am using the below code for list of installed applications in the device.

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
    ArrayList<PInfo> res = new ArrayList<PInfo>();

    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

    for (int i=0; i<packs.size(); i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        PInfo newInfo = new PInfo();
        newInfo.setAppname(p.applicationInfo.loadLabel(getPackageManager()).toString());
        newInfo.setPname(p.packageName);
        newInfo.setVersionName(p.versionName);
        newInfo.setVersionCode(p.versionCode);
        newInfo.setIcon(p.applicationInfo.loadIcon(getPackageManager()));

        res.add(newInfo);
    }
    return res;
}

How do I know what's the data used by each application?

Actually, I need a solution which gives data usage of applications in a given time period, i.e. in between two days.


回答1:


First, get a list of all running apps' process info:

List<RunningAppProcessInfo>

Then get the UID of every app and get then send and receive traffic of the app:

// Get running processes
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningApps = manager.getRunningAppProcesses();

for (RunningAppProcessInfo runningApp : runningApps) {

  // Get UID of the selected process
  int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;

  // Get traffic data
  long received = TrafficStats.getUidRxBytes(uid);
  long send   = TrafficStats.getUidTxBytes(uid);
  Log.v("" + uid , "Send :" + send + ", Received :" + received);
}



回答2:


You can use the android.net.TrafficStats for getting the network usage details.

Please find a sample program below for the same.

package com.anchit.trafficstatus;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class TrafficStatus extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Log.e("bytes recvd", "" + android.net.TrafficStats.getMobileRxBytes());

        Log.e("Total", "Bytes received" + android.net.TrafficStats.getTotalRxBytes());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}



回答3:


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<ActivityManager.RunningAppProcessInfo> 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));
    }
}


来源:https://stackoverflow.com/questions/17674790/how-do-i-programmatically-show-data-usage-of-all-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!