【Android】(七) TrafficStatistics API

在这里插入图片描述

1 统计总计流量

/**
 * Return number of packets transmitted since device boot. Counts packets
 * across all network interfaces, and always increases monotonically since
 * device boot. Statistics are measured at the network layer, so they
 * include both TCP and UDP usage.
 */
public static long getTotalTxPackets()

/**
 * Return number of packets received since device boot. Counts packets
 * across all network interfaces, and always increases monotonically since
 * device boot. Statistics are measured at the network layer, so they
 * include both TCP and UDP usage.
 */
public static long getTotalRxPackets()

/**
 * Return number of bytes transmitted since device boot. Counts packets
 * across all network interfaces, and always increases monotonically since
 * device boot. Statistics are measured at the network layer, so they
 * include both TCP and UDP usage.
 */
public static long getTotalTxBytes()

/**
 * Return number of bytes received since device boot. Counts packets across
 * all network interfaces, and always increases monotonically since device
 * boot. Statistics are measured at the network layer, so they include both
 * TCP and UDP usage.
 */
public static long getTotalRxBytes()

2 统计移动网络流量

/**
 * Return number of packets transmitted across mobile networks since device
 * boot. Counts packets across all mobile network interfaces, and always
 * increases monotonically since device boot. Statistics are measured at the
 * network layer, so they include both TCP and UDP usage.
 */
public static long getMobileTxPackets()

/**
 * Return number of packets received across mobile networks since device
 * boot. Counts packets across all mobile network interfaces, and always
 * increases monotonically since device boot. Statistics are measured at the
 * network layer, so they include both TCP and UDP usage.
 */
public static long getMobileRxPackets()

/**
 * Return number of bytes transmitted across mobile networks since device
 * boot. Counts packets across all mobile network interfaces, and always
 * increases monotonically since device boot. Statistics are measured at the
 * network layer, so they include both TCP and UDP usage.
 */
public static long getMobileTxBytes()

/**
 * Return number of bytes received across mobile networks since device boot.
 * Counts packets across all mobile network interfaces, and always increases
 * monotonically since device boot. Statistics are measured at the network
 * layer, so they include both TCP and UDP usage.
 */
public static long getMobileRxBytes()

3 按接口统计流量

/**
 * Return the number of packets transmitted on the specified interface since the interface
 * was created. Statistics are measured at the network layer, so both TCP and
 * UDP usage are included.
 *
 * Note that the returned values are partial statistics that do not count data from several
 * sources and do not apply several adjustments that are necessary for correctness, such
 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
 * determine whether traffic is being transferred on the specific interface but are not a
 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
 * APIs.
 */
public static long getTxPackets(@NonNull String iface)

/**
 * Return the number of packets received on the specified interface since the interface was
 * created. Statistics are measured at the network layer, so both TCP
 * and UDP usage are included.
 *
 * Note that the returned values are partial statistics that do not count data from several
 * sources and do not apply several adjustments that are necessary for correctness, such
 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
 * determine whether traffic is being transferred on the specific interface but are not a
 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
 * APIs.
 */
public static long getRxPackets(@NonNull String iface)

/**
 * Return the number of bytes transmitted on the specified interface since the interface
 * was created. Statistics are measured at the network layer, so both TCP and
 * UDP usage are included.
 *
 * Note that the returned values are partial statistics that do not count data from several
 * sources and do not apply several adjustments that are necessary for correctness, such
 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
 * determine whether traffic is being transferred on the specific interface but are not a
 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
 * APIs.
 */
public static long getTxBytes(@NonNull String iface)

/**
 * Return the number of bytes received on the specified interface since the interface
 * was created. Statistics are measured at the network layer, so both TCP
 * and UDP usage are included.
 *
 * Note that the returned values are partial statistics that do not count data from several
 * sources and do not apply several adjustments that are necessary for correctness, such
 * as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to
 * determine whether traffic is being transferred on the specific interface but are not a
 * substitute for the more accurate statistics provided by the {@link NetworkStatsManager}
 * APIs.
 */
public static long getRxBytes(@NonNull String iface) 

4 按UID统计流量

/**
 * Return number of bytes transmitted by the given UID since device boot.
 * Counts packets across all network interfaces, and always increases
 * monotonically since device boot. Statistics are measured at the network
 * layer, so they include both TCP and UDP usage.
 */
public static long getUidTxBytes(int uid)

/**
 * Return number of bytes received by the given UID since device boot.
 * Counts packets across all network interfaces, and always increases
 * monotonically since device boot. Statistics are measured at the network
 * layer, so they include both TCP and UDP usage.
 */
public static long getUidRxBytes(int uid)

/**
 * Return number of packets transmitted by the given UID since device boot.
 * Counts packets across all network interfaces, and always increases
 * monotonically since device boot. Statistics are measured at the network
 * layer, so they include both TCP and UDP usage.
 */
public static long getUidTxPackets(int uid)

/**
 * Return number of packets received by the given UID since device boot.
 * Counts packets across all network interfaces, and always increases
 * monotonically since device boot. Statistics are measured at the network
 * layer, so they include both TCP and UDP usage.
 */
public static long getUidRxPackets(int uid)

5 按时间段统计流量

/**
 * Query network usage statistics summaries. Result is summarised data usage for the whole
 * device. Result is a single Bucket aggregated over time, state, uid, tag, metered, and
 * roaming. This means the bucket's start and end timestamp are going to be the same as the
 * 'startTime' and 'endTime' parameters. State is going to be
 * {@link NetworkStats.Bucket#STATE_ALL}, uid {@link NetworkStats.Bucket#UID_ALL},
 * tag {@li
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值