Google is committed to advancing racial equity for Black communities. See how.
Added in API level 23

NetworkStatsManager

open class NetworkStatsManager
kotlin.Any
   ↳ android.app.usage.NetworkStatsManager

Provides access to network usage history and statistics. Usage data is collected in discrete bins of time called 'Buckets'. See NetworkStats.Bucket for details.

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

querySummaryForUser

querySummary

These queries aggregate network usage across the whole interval. Therefore there will be only one bucket for a particular key, state, metered 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

queryDetails

These queries do not aggregate over time but do aggregate over state, metered and roaming. Therefore there can be multiple buckets for a particular key. However, all Buckets will have state NetworkStats.Bucket#STATE_ALL, defaultNetwork NetworkStats.Bucket#DEFAULT_NETWORK_ALL, metered NetworkStats.Bucket#METERED_ALL, roaming NetworkStats.Bucket#ROAMING_ALL.

NOTE: Calling querySummaryForDevice or accessing stats for apps other than the calling app requires the permission android.Manifest.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). 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 {@value android.os.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

abstract

Base class for usage callbacks.

Public methods

open NetworkStats!
queryDetails(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long)

Query network usage statistics details.

open NetworkStats!
queryDetailsForUid(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long, uid: Int)

Query network usage statistics details for a given uid.

open NetworkStats!
queryDetailsForUidTag(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long, uid: Int, tag: Int)

Query network usage statistics details for a given uid and tag.

open NetworkStats!
queryDetailsForUidTagState(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long, uid: Int, tag: Int, state: Int)

Query network usage statistics details for a given uid, tag, and state.

open NetworkStats!
querySummary(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long)

Query network usage statistics summaries.

open NetworkStats.Bucket!
querySummaryForDevice(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long)

Query network usage statistics summaries.

open NetworkStats.Bucket!
querySummaryForUser(networkType: Int, subscriberId: String!, startTime: Long, endTime: Long)

Query network usage statistics summaries.

open Unit
registerUsageCallback(networkType: Int, subscriberId: String!, thresholdBytes: Long, callback: NetworkStatsManager.UsageCallback!)

Registers to receive notifications about data usage on specified networks.

open Unit
registerUsageCallback(networkType: Int, subscriberId: String!, thresholdBytes: Long, callback: NetworkStatsManager.UsageCallback!, handler: Handler?)

Registers to receive notifications about data usage on specified networks.

open Unit

Unregisters callbacks on data usage.

Public methods

queryDetails

Added in API level 23
open fun queryDetails(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long
): NetworkStats!

Query network usage statistics details. Result filtered to include only uids belonging to calling user. Result is aggregated over state but not aggregated over time, uid, tag, metered, nor roaming. This means buckets' start and end timestamps are going to be between 'startTime' and 'endTime' parameters. State is going to be NetworkStats.Bucket#STATE_ALL, uid will vary, tag NetworkStats.Bucket#TAG_NONE, default network is going to be NetworkStats.Bucket#DEFAULT_NETWORK_ALL, metered is going to be NetworkStats.Bucket#METERED_ALL, and roaming is going to be NetworkStats.Bucket#ROAMING_ALL.

Only includes buckets that atomically occur in the inclusive time range. Doesn't interpolate across partial buckets. Since bucket length is in the order of hours, this method cannot be used to measure data usage on a fine grained time scale.

Parameters
networkType Int: As defined in ConnectivityManager, e.g. ConnectivityManager#TYPE_MOBILE, ConnectivityManager#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 java.lang.System#currentTimeMillis.
endTime Long: End of period. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
NetworkStats! Statistics object or null if permissions are insufficient or error happened during statistics collection.

queryDetailsForUid

Added in API level 23
open fun queryDetailsForUid(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long,
    uid: Int
): NetworkStats!

Query network usage statistics details for a given uid. #see queryDetailsForUidTagState(int, String, long, long, int, int, int)

queryDetailsForUidTag

Added in API level 24
open fun queryDetailsForUidTag(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long,
    uid: Int,
    tag: Int
): NetworkStats!

Query network usage statistics details for a given uid and tag. #see queryDetailsForUidTagState(int, String, long, long, int, int, int)

queryDetailsForUidTagState

Added in API level 28
open fun queryDetailsForUidTagState(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long,
    uid: Int,
    tag: Int,
    state: Int
): NetworkStats!

Query network usage statistics details for a given uid, tag, and state. Only usable for uids belonging to calling user. Result is not aggregated over time. This means buckets' start and end timestamps are going to be between 'startTime' and 'endTime' parameters. The uid is going to be the same as the 'uid' parameter, the tag the same as the 'tag' parameter, and the state the same as the 'state' parameter. defaultNetwork is going to be NetworkStats.Bucket#DEFAULT_NETWORK_ALL, metered is going to be NetworkStats.Bucket#METERED_ALL, and roaming is going to be NetworkStats.Bucket#ROAMING_ALL.

Only includes buckets that atomically occur in the inclusive time range. Doesn't interpolate across partial buckets. Since bucket length is in the order of hours, this method cannot be used to measure data usage on a fine grained time scale.

Parameters
networkType Int: As defined in ConnectivityManager, e.g. ConnectivityManager#TYPE_MOBILE, ConnectivityManager#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 java.lang.System#currentTimeMillis.
endTime Long: End of period. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
uid Int: UID of app
tag Int: TAG of interest. Use NetworkStats.Bucket#TAG_NONE for no tags.
state Int: state of interest. Use NetworkStats.Bucket#STATE_ALL to aggregate traffic from all states.
Return
NetworkStats! Statistics object or null if an error happened during statistics collection.
Exceptions
java.lang.SecurityException if permissions are insufficient to read network statistics.

querySummary

Added in API level 23
open fun querySummary(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long
): NetworkStats!

Query network usage statistics summaries. Result filtered to include only uids belonging to calling user. Result is aggregated over time, hence all buckets will have the same start and end timestamps. Not aggregated over state, uid, default network, metered, or roaming. This means buckets' start and end timestamps are going to be the same as the 'startTime' and 'endTime' parameters. State, uid, metered, and roaming are going to vary, and tag is going to be the same.

Parameters
networkType Int: As defined in ConnectivityManager, e.g. ConnectivityManager#TYPE_MOBILE, ConnectivityManager#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 java.lang.System#currentTimeMillis.
endTime Long: End of period. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
NetworkStats! Statistics object or null if permissions are insufficient or error happened during statistics collection.

querySummaryForDevice

Added in API level 23
open fun querySummaryForDevice(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long
): NetworkStats.Bucket!

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 NetworkStats.Bucket#STATE_ALL, uid NetworkStats.Bucket#UID_ALL, tag NetworkStats.Bucket#TAG_NONE, default network NetworkStats.Bucket#DEFAULT_NETWORK_ALL, metered NetworkStats.Bucket#METERED_ALL, and roaming NetworkStats.Bucket#ROAMING_ALL.

Parameters
networkType Int: As defined in ConnectivityManager, e.g. ConnectivityManager#TYPE_MOBILE, ConnectivityManager#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 java.lang.System#currentTimeMillis.
endTime Long: End of period. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
NetworkStats.Bucket! Bucket object or null if permissions are insufficient or error happened during statistics collection.

querySummaryForUser

Added in API level 23
open fun querySummaryForUser(
    networkType: Int,
    subscriberId: String!,
    startTime: Long,
    endTime: Long
): NetworkStats.Bucket!

Query network usage statistics summaries. Result is summarised data usage for all uids belonging to calling user. Result is a single Bucket aggregated over time, state and uid. 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 NetworkStats.Bucket#STATE_ALL, uid NetworkStats.Bucket#UID_ALL, tag NetworkStats.Bucket#TAG_NONE, metered NetworkStats.Bucket#METERED_ALL, and roaming NetworkStats.Bucket#ROAMING_ALL.

Parameters
networkType Int: As defined in ConnectivityManager, e.g. ConnectivityManager#TYPE_MOBILE, ConnectivityManager#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 java.lang.System#currentTimeMillis.
endTime Long: End of period. Defined in terms of "Unix time", see java.lang.System#currentTimeMillis.
Return
NetworkStats.Bucket! Bucket object or null if permissions are insufficient or error happened during statistics collection.

registerUsageCallback

Added in API level 24
open fun registerUsageCallback(
    networkType: Int,
    subscriberId: String!,
    thresholdBytes: Long,
    callback: NetworkStatsManager.UsageCallback!
): Unit

Registers to receive notifications about data usage on specified networks. #see registerUsageCallback(int, String[], long, UsageCallback, Handler)

registerUsageCallback

Added in API level 24
open fun registerUsageCallback(
    networkType: Int,
    subscriberId: String!,
    thresholdBytes: Long,
    callback: NetworkStatsManager.UsageCallback!,
    handler: Handler?
): Unit

Registers to receive notifications about data usage on specified networks.

The callbacks will continue to be called as long as the process is live or unregisterUsageCallback is called.

Parameters
networkType Int: Type of network to monitor. Either ConnectivityManager#TYPE_MOBILE or ConnectivityManager#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 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. This value may be null.

unregisterUsageCallback

Added in API level 24
open fun unregisterUsageCallback(callback: NetworkStatsManager.UsageCallback!): Unit

Unregisters callbacks on data usage.

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