Most visited

Recently visited

Added in API level 23

NetworkStatsManager

public class NetworkStatsManager
extends Object

java.lang.Object
   ↳ android.app.usage.NetworkStatsManager


提供对网络使用历史和统计数据的访问。 使用数据收集在称为“桶”的离散时间仓中。 有关详细信息,请参阅NetworkStats.Bucket

Queries can define a time interval in the form of start and end timestamps (Long.MIN_VALUE and Long.MAX_VALUE can be used to simulate open ended intervals). By default, apps can only obtain data about themselves. See the below note for special cases in which apps can obtain data about other applications.

Summary queries

querySummaryForDevice(int, String, long, long)

querySummaryForUser(int, String, long, long)

querySummary(int, String, long, long)

These queries aggregate network usage across the whole interval. Therefore there will be only one bucket for a particular key and state and roaming combination. In case of the user-wide and device-wide summaries a single bucket containing the totalised network usage is returned.

History queries

queryDetailsForUid(int, String, long, long, int)

queryDetails(int, String, long, long)

These queries do not aggregate over time but do aggregate over state and roaming. Therefore there can be multiple buckets for a particular key but all Bucket's state is going to be STATE_ALL and all Bucket's roaming is going to be ROAMING_ALL.

NOTE: Calling querySummaryForDevice(int, String, long, long) or accessing stats for apps other than the calling app requires the permission PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.

Profile owner apps are automatically granted permission to query data on the profile they manage (that is, for any query except querySummaryForDevice(int, String, long, long)). Device owner apps and carrier- privileged apps likewise get access to usage data for all users on the device.

In addition to tethering usage, usage by removed users and apps, and usage by the system is also included in the results for callers with one of these higher levels of access.

NOTE: Prior to API level Build.VERSION_CODES.N, all calls to these APIs required the above permission, even to access an app's own data usage, and carrier-privileged apps were not included.

Summary

Nested classes

class NetworkStatsManager.UsageCallback

使用回调的基类。

Public methods

NetworkStats queryDetails(int networkType, String subscriberId, long startTime, long endTime)

查询网络使用情况统计信息

NetworkStats queryDetailsForUid(int networkType, String subscriberId, long startTime, long endTime, int uid)

查询给定uid的网络使用情况统计信息。

NetworkStats queryDetailsForUidTag(int networkType, String subscriberId, long startTime, long endTime, int uid, int tag)

查询给定uid和标签的网络使用情况统计信息。

NetworkStats querySummary(int networkType, String subscriberId, long startTime, long endTime)

查询网络使用情况统计摘要。

NetworkStats.Bucket querySummaryForDevice(int networkType, String subscriberId, long startTime, long endTime)

查询网络使用情况统计摘要。

NetworkStats.Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, long endTime)

查询网络使用情况统计摘要。

void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, NetworkStatsManager.UsageCallback callback, Handler handler)

注册接收关于指定网络上数据使用情况的通知。

void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, NetworkStatsManager.UsageCallback callback)

注册接收关于指定网络上数据使用情况的通知。

void unregisterUsageCallback(NetworkStatsManager.UsageCallback callback)

取消注册数据使用回调。

Inherited methods

From class java.lang.Object

Public methods

queryDetails

Added in API level 23
NetworkStats queryDetails (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime)

查询网络使用情况统计信息 结果已过滤,只包含属于主叫用户的用户。 结果是根据状态汇总的,但不会随着时间或uid汇总。 这意味着存储桶的开始和结束时间戳将在'startTime'和'endTime'参数之间。 状态将是STATE_ALL ,uid会有所不同,标记TAG_NONE和漫游将是ROAMING_ALL

只包括在包含时间范围内原子发生的桶。 不会插入部分存储桶中。 由于存储桶长度为小时数,因此此方法不能用于在细粒度的时间范围内测量数据使用情况。

Parameters
networkType int: As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId String: If applicable, the subscriber id of the network interface.
startTime long: Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime long: End of period. Defined in terms of "Unix time", see currentTimeMillis().
Returns
NetworkStats Statistics object or null if permissions are insufficient or error happened during statistics collection.
Throws
SecurityException
RemoteException

queryDetailsForUid

Added in API level 23
NetworkStats queryDetailsForUid (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime, 
                int uid)

查询给定uid的网络使用情况统计信息。 #see queryDetailsForUidTag(int,String,long,long,int,int)

Parameters
networkType int
subscriberId String
startTime long
endTime long
uid int
Returns
NetworkStats
Throws
SecurityException
RemoteException

queryDetailsForUidTag

Added in API level 24
NetworkStats queryDetailsForUidTag (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime, 
                int uid, 
                int tag)

查询给定uid和标签的网络使用情况统计信息。 仅适用于属于主叫用户的用户。 结果按状态汇总,但不会随时间汇总。 这意味着存储桶的开始和结束时间戳将在'startTime'和'endTime'参数之间。 状态将是STATE_ALL ,与'uid'参数相同并标记为'tag'参数。

只包括在包含时间范围内原子发生的桶。 不会插入部分存储桶中。 由于存储桶长度为小时数,因此此方法不能用于在细粒度的时间范围内测量数据使用情况。

Parameters
networkType int: As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId String: If applicable, the subscriber id of the network interface.
startTime long: Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime long: End of period. Defined in terms of "Unix time", see currentTimeMillis().
uid int: UID of app
tag int: TAG of interest. Use TAG_NONE for no tags.
Returns
NetworkStats Statistics object or null if an error happened during statistics collection.
Throws
SecurityException if permissions are insufficient to read network statistics.

querySummary

Added in API level 23
NetworkStats querySummary (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime)

查询网络使用情况统计摘要。 结果已过滤,只包含属于主叫用户的用户。 结果随时间汇总,因此所有存储桶将具有相同的开始和结束时间戳。 不是聚合在状态或uid上。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同。 状态和uid会有所不同,并且标签将会相同。

Parameters
networkType int: As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId String: If applicable, the subscriber id of the network interface.
startTime long: Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime long: End of period. Defined in terms of "Unix time", see currentTimeMillis().
Returns
NetworkStats Statistics object or null if permissions are insufficient or error happened during statistics collection.
Throws
SecurityException
RemoteException

querySummaryForDevice

Added in API level 23
NetworkStats.Bucket querySummaryForDevice (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime)

查询网络使用情况统计摘要。 结果是整个设备的总结数据使用情况。 结果是一个单独的Bucket随着时间,状态,uid,标签和漫游而聚合。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同。 状态将是STATE_ALL ,uid UID_ALL ,标记TAG_NONE和漫游ROAMING_ALL

Parameters
networkType int: As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId String: If applicable, the subscriber id of the network interface.
startTime long: Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime long: End of period. Defined in terms of "Unix time", see currentTimeMillis().
Returns
NetworkStats.Bucket Bucket object or null if permissions are insufficient or error happened during statistics collection.
Throws
SecurityException
RemoteException

querySummaryForUser

Added in API level 23
NetworkStats.Bucket querySummaryForUser (int networkType, 
                String subscriberId, 
                long startTime, 
                long endTime)

查询网络使用情况统计摘要。 结果是属于主叫用户的所有用户的汇总数据使用情况。 结果是一个单独的Bucket随着时间的推移而聚合,状态和uid。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同,状态将是STATE_ALL和uid UID_ALL

Parameters
networkType int: As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId String: If applicable, the subscriber id of the network interface.
startTime long: Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime long: End of period. Defined in terms of "Unix time", see currentTimeMillis().
Returns
NetworkStats.Bucket Bucket object or null if permissions are insufficient or error happened during statistics collection.
Throws
SecurityException
RemoteException

registerUsageCallback

Added in API level 24
void registerUsageCallback (int networkType, 
                String subscriberId, 
                long thresholdBytes, 
                NetworkStatsManager.UsageCallback callback, 
                Handler handler)

注册接收关于指定网络上数据使用情况的通知。

只要过程处于活动状态或 unregisterUsageCallback(NetworkStatsManager.UsageCallback) ,回调就会继续被调用。

Parameters
networkType int: Type of network to monitor. Either TYPE_MOBILE or TYPE_WIFI.
subscriberId String: If applicable, the subscriber id of the network interface.
thresholdBytes long: Threshold in bytes to be notified on.
callback NetworkStatsManager.UsageCallback: The NetworkStatsManager.UsageCallback that the system will call when data usage has exceeded the specified threshold.
handler Handler: to dispatch callback events through, otherwise if null it uses the calling thread.

registerUsageCallback

Added in API level 24
void registerUsageCallback (int networkType, 
                String subscriberId, 
                long thresholdBytes, 
                NetworkStatsManager.UsageCallback callback)

注册接收关于指定网络上数据使用情况的通知。 #see registerUsageCallback(int,String [],long,UsageCallback,Handler)

Parameters
networkType int
subscriberId String
thresholdBytes long
callback NetworkStatsManager.UsageCallback

unregisterUsageCallback

Added in API level 24
void unregisterUsageCallback (NetworkStatsManager.UsageCallback callback)

取消注册数据使用回调。

Parameters
callback NetworkStatsManager.UsageCallback: The NetworkStatsManager.UsageCallback used when registering.

Hooray!