是否有任何可能的方法可以使用 TrafficStats 计算 android 中每个应用程序的移动和 wifi 使用情况: (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) 方法?我知道一定有某种方法可以做到这一点,因为 3G 看门狗和其他一些应用程序提供了这些细节。
有人可以帮忙吗?谢谢
是否有任何可能的方法可以使用 TrafficStats 计算 android 中每个应用程序的移动和 wifi 使用情况: (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) 方法?我知道一定有某种方法可以做到这一点,因为 3G 看门狗和其他一些应用程序提供了这些细节。
有人可以帮忙吗?谢谢
我在这里为此类问题写了一个类似的答案。要获取每个应用程序的总体数据使用情况,使用您命名的方法非常容易,TrafficStats.getUidTxBytes(int)
并且TrafficStats.getUidRxBytes(int)
. 但是,目前尚未公开进入移动和 Wifi 领域。
正如我在其他答案中提到的,您必须深入研究内部框架 API 并尝试复制其中使用的一些方法。您肯定需要使用NetworkTemplate.buildTemplateWifiWildcard()
and来为每个接口NetworkTemplate.buildTemplateMobileWildcard()
检索正确的。NetworkStats
一旦你有了NetworkStats
对象,你就可以得到你的信息。请注意:如果您在公共应用程序中使用它,那么每当 Google 决定更新它们时,使用内部 API很可能会中断。
编辑:作为另一个提示,您应该查看设置应用程序如何显示每个应用程序的流量并通过移动/Wifi 将其分开的来源。特别是你可以在这里查看他们的ChartDataLoader
,特别是方法collectHistoryForUid()
和loadInBackground()
Android 框架中有一个隐藏类,叫做 NetworkStats:
/**
* Collection of active network statistics. Can contain summary details across
* all interfaces, or details with per-UID granularity. Internally stores data
* as a large table, closely matching {@code /proc/} data format. This structure
* optimizes for rapid in-memory comparison, but consider using
* {@link NetworkStatsHistory} when persisting.
*
* @hide
*/
public class NetworkStats implements Parcelable
它有一个方法叫做
/**
* Return total statistics grouped by {@link #iface}; doesn't mutate the
* original structure.
*/
public NetworkStats groupedByIface()
而且我想这种方法可以满足您的要求。您可能需要参考源代码并查看是否可以提取有用信息用于您自己的目的。
经过长时间的努力,我能够找到通过任何接口为每个已安装应用程序在 android 设备中获取数据的解决方案。
由于 Android 提供了 TrafficStats API,但这些 API 为每个应用程序 uid 提供了完整的数据统计信息,因为设备启动并且甚至 API 不支持通过任何接口获取特定应用程序的数据。即使我们依赖 TraffiucStates APIS,我们也会为每个应用程序获得一个新的数据统计信息。
所以我想使用隐藏的 API 来使用这个..
在这里,我提到了通过 Android 中的任何接口获取每个应用程序的数据统计信息的步骤...
建立“INetworkStatsSession”会话
import android.net.INetworkStatsSession;
INetworkStatsSession mStatsSession = mStatsService.openSession();
根据您要测量的接口创建一个网络模板..
import static android.net.NetworkTemplate.buildTemplateEthernet;
import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
import static android.net.NetworkTemplate.buildTemplateMobile4g;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import android.net.NetworkTemplate;
private NetworkTemplate mTemplate;
mTemplate = buildTemplateMobileAll(getActiveSubscriberId(this
.getApplicationContext()));
获取活动订阅者 ID:
private static String getActiveSubscriberId(Context context) {
final TelephonyManager tele = TelephonyManager.from(context);
final String actualSubscriberId = tele.getSubscriberId();
return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
}
通过传递应用程序 UID 收集各个应用程序的网络历史记录...
private NetworkStatsHistory collectHistoryForUid(NetworkTemplate template,
int uid, int set) throws RemoteException {
final NetworkStatsHistory history = mStatsSession.getHistoryForUid(
template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
return history;
}
获取总消费数据:
public void showConsuption(int UID){
NetworkStatsHistory history = collectHistoryForUid(mTemplate, UID, SET_DEFAULT);
Log.i(DEBUG_TAG, "load:::::SET_DEFAULT:.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
history = collectHistoryForUid(mTemplate, 10093, SET_FOREGROUND);
Log.i(DEBUG_TAG, "load::::SET_FOREGROUND::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
history = collectHistoryForUid(mTemplate, 10093, SET_ALL);
Log.i(DEBUG_TAG, "load::::SET_ALL::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes()));
}