如何以编程方式获取android设备的总数据使用率?

如何以编程方式获取android设备的总数据使用率?,android,usage-statistics,android-data-usage,Android,Usage Statistics,Android Data Usage,在我的android智能手机的设置菜单中,我可以看到当前和过去周期的总数据使用量。我可以通过编程方式在我的应用程序中检索这些信息吗 提前感谢。您可以使用android.net.TrafficStats获取流量详细信息: 例如,要获取接收的总字节数: android.net.TrafficStats.getTotalRxBytes() 如果您想一个接一个地获取应用程序的发送和接收信息,您可以这样获取: 首先通过以下方式获取所有应用程序的运行信息: List<RunningAppProces

在我的android智能手机的设置菜单中,我可以看到当前和过去周期的总数据使用量。我可以通过编程方式在我的应用程序中检索这些信息吗


提前感谢。

您可以使用android.net.TrafficStats获取流量详细信息:

例如,要获取接收的总字节数:

android.net.TrafficStats.getTotalRxBytes()
如果您想一个接一个地获取应用程序的发送和接收信息,您可以这样获取:

首先通过以下方式获取所有应用程序的运行信息:

List<RunningAppProcessInfo>
列表
然后获取你想要的每个应用程序的UID

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;

  long received = TrafficStats.getUidRxBytes(uid);//received amount of each app
  long send   = TrafficStats.getUidTxBytes(uid);//sent amount of each app
}
ActivityManager=(ActivityManager)getSystemService(ACTIVITY_服务);
List runningApps=manager.getrunningappprocesss();
对于(RunningAppProcessInfo runningApp:runningApps){
//获取所选进程的UID
int uid=((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;
long received=TrafficStats.getUidRxBytes(uid);//每个应用的接收量
long send=TrafficStats.getUidTxBytes(uid);//每个应用的发送量
}

让我知道这是你想要的吗

但这只提供了当前运行的应用程序的使用情况,不是吗?答案的第二部分,是的,但这个android.net.TrafficStats.getTotalRxBytes()将提供你的总使用量,但只有在上次设备启动之后。但查找它会让我找到NetworkStatsManager,我认为这是我的解决方案。将在以后对其进行测试,可能重复