NetworkStatsManager
public class NetworkStatsManager
extends Object
提供对网络使用历史和统计数据的访问。 使用数据收集在称为“桶”的离散时间仓中。 有关详细信息,请参阅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
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) 取消注册数据使用回调。 |
Public methods
queryDetails
NetworkStats queryDetails (int networkType,
String subscriberId,
long startTime,
long endTime)
查询网络使用情况统计信息 结果已过滤,只包含属于主叫用户的用户。 结果是根据状态汇总的,但不会随着时间或uid汇总。 这意味着存储桶的开始和结束时间戳将在'startTime'和'endTime'参数之间。 状态将是STATE_ALL
,uid会有所不同,标记TAG_NONE
和漫游将是ROAMING_ALL
。
只包括在包含时间范围内原子发生的桶。 不会插入部分存储桶中。 由于存储桶长度为小时数,因此此方法不能用于在细粒度的时间范围内测量数据使用情况。
Returns |
NetworkStats |
Statistics object or null if permissions are insufficient or error happened during statistics collection. |
queryDetailsForUid
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
|
queryDetailsForUidTag
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. |
querySummary
NetworkStats querySummary (int networkType,
String subscriberId,
long startTime,
long endTime)
查询网络使用情况统计摘要。 结果已过滤,只包含属于主叫用户的用户。 结果随时间汇总,因此所有存储桶将具有相同的开始和结束时间戳。 不是聚合在状态或uid上。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同。 状态和uid会有所不同,并且标签将会相同。
Returns |
NetworkStats |
Statistics object or null if permissions are insufficient or error happened during statistics collection. |
querySummaryForDevice
NetworkStats.Bucket querySummaryForDevice (int networkType,
String subscriberId,
long startTime,
long endTime)
查询网络使用情况统计摘要。 结果是整个设备的总结数据使用情况。 结果是一个单独的Bucket随着时间,状态,uid,标签和漫游而聚合。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同。 状态将是STATE_ALL
,uid UID_ALL
,标记TAG_NONE
和漫游ROAMING_ALL
。
Returns |
NetworkStats.Bucket |
Bucket object or null if permissions are insufficient or error happened during statistics collection. |
querySummaryForUser
NetworkStats.Bucket querySummaryForUser (int networkType,
String subscriberId,
long startTime,
long endTime)
查询网络使用情况统计摘要。 结果是属于主叫用户的所有用户的汇总数据使用情况。 结果是一个单独的Bucket随着时间的推移而聚合,状态和uid。 这意味着桶的开始和结束时间戳将与'startTime'和'endTime'参数相同,状态将是STATE_ALL
和uid UID_ALL
。
Returns |
NetworkStats.Bucket |
Bucket object or null if permissions are insufficient or error happened during statistics collection. |
registerUsageCallback
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
|