如何以编程方式显示所有应用程序的数据使用情况?

Kir*_*n_b 16 android usage-statistics android-data-usage

在Android 4.0以上,我们在手机中有数据使用控制选项.请查看随附的屏幕截图以进一步了解.

http://developer.android.com/about/versions/android-4.0-highlights.html

现在我需要从我的应用程序中检查这些内容(特定时间段/特定日期内所有应用程序的数据使用情况).我怎样才能做到这一点?我也使用以下类来了解网络使用情况.

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

请检查以下链接图像.我需要开发相同类型的应用程序.

感谢您分享您的代码,但我需要知道每个应用程序使用的数据而不是所有应用程序.到目前为止,我在链接中观察到没有人在讨论各个应用程序的数据使用情况.我已经知道如何在设备中显示已安装的应用程序.现在我想知道每个应用程序使用的数据是什么.

我使用以下代码列出设备中已安装的应用程序列表.

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;
}
Run Code Online (Sandbox Code Playgroud)

我如何知道每个应用程序使用的数据是什么?

实际上,我需要一个解决方案,在给定的时间段内(即两天之间)提供应用程序的数据使用.

Aru*_*dra 9

首先,获取所有正在运行的应用程序的进程信息列表:

List<RunningAppProcessInfo>
Run Code Online (Sandbox Code Playgroud)

然后获取每个应用程序的UID,然后发送和接收应用程序的流量:

// 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);
}
Run Code Online (Sandbox Code Playgroud)


Anc*_*tal 6

您可以使用它android.net.TrafficStats来获取网络使用详细信息.

请在下面找到相同的示例程序.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)