如何以编程方式获取Android设备的总数据用量?

问题描述 投票:0回答:1

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

提前致谢。

android usage-statistics android-data-usage
1个回答
0
投票

您可以使用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
}

让我知道这就是你想要的

© www.soinside.com 2019 - 2024. All rights reserved.