如何计算android中每个应用程序的移动和wifi数据使用情况?

问题描述 投票:5回答:3

有没有办法使用TrafficStats'(getUidRxBytes,getUidTxBytes,getTotalRxbytes,getTotalTXbytes,getMobileRxBytes,getMobileTxBytes)方法计算Android中每个应用程序的移动和wifi使用情况?我知道必须有一些方法可以做到这一点,因为3G看门狗和其他一些应用程序提供了这些细节。

有人可以帮忙吗?谢谢

android usage-statistics android-data-usage
3个回答
6
投票

我已经为这类问题here写了类似的答案。要获得每个应用程序的整体数据使用情况,使用您命名的方法TrafficStats.getUidTxBytes(int)TrafficStats.getUidRxBytes(int)相当容易。然而,打入移动和Wifi并不是目前公开的东西。

正如我在其他答案中提到的那样,您将不得不深入研究内部框架API并尝试复制其中使用的一些方法。你肯定需要使用NetworkTemplate.buildTemplateWifiWildcard()NetworkTemplate.buildTemplateMobileWildcard()来为每个接口检索正确的NetworkStats。一旦你有NetworkStats对象,你将能够获得你的信息。请注意:如果您在公共应用中使用此功能,那么每当Google决定更新它们时,使用内部API很可能会中断。

编辑:作为另一个提示,您应该查看设置应用程序如何能够显示每个应用程序的流量并通过移动/ Wifi分隔它的来源。特别是你可以看看他们的ChartDataLoader,特别是方法collectHistoryForUid()loadInBackground()


-1
投票

经过长期的努力,我能够找到解决方案,通过Android设备中每个已安装的应用程序的任何界面获取数据。

由于Android提供TrafficStats Apis,但这些API为每个应用程序提供了完整的数据存储,因为设备启动和Even API不支持通过特定应用程序的任何接口获取数据。即使我们依赖TraffiucStates APIS,我们也会为每个应用程序获得一个新的数据统计数据。

所以我想用隐藏的API来使用它。

在这里,我提到了通过Android中的任何接口获取每个应用程序的数据统计信息的步骤...

  1. 建立“INetworkStatsSession”会议 import android.net.INetworkStatsSession; INetworkStatsSession mStatsSession = mStatsService.openSession();
  2. 根据您要测量的界面创建网络模板。 qazxsw poi
  3. GetActive SubcriberID: 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()));
  4. 通过传递应用程序ID收集相应应用程序的网络HIStory ... qazxsw poi
  5. 获取总消耗数据: private static String getActiveSubscriberId(Context context) { final TelephonyManager tele = TelephonyManager.from(context); final String actualSubscriberId = tele.getSubscriberId(); return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); }

-2
投票

Android框架中有一个名为NetworkStats的隐藏类:

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()));

}

我想这种方法可以满足您的要求。您可能需要参考源代码并查看是否可以为自己的目的提取有用的信息。

/** * 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

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