Android APIs
public class

NetworkStatsManager

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

Class Overview

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). All queries (except querySummaryForDevice(int, String, long, long)) collect only network usage of apps belonging to the same user as the client. In addition tethering usage, usage by removed users and apps, and usage by system is also included in the results.

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 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. Therefore there can be multiple buckets for a particular key but all Bucket's state is going to be STATE_ALL.

NOTE: This API 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 likewise get access to usage data of the primary user.

Summary

Public Methods
NetworkStats queryDetails(int networkType, String subscriberId, long startTime, long endTime)
Query network usage statistics details.
NetworkStats queryDetailsForUid(int networkType, String subscriberId, long startTime, long endTime, int uid)
Query network usage statistics details.
NetworkStats querySummary(int networkType, String subscriberId, long startTime, long endTime)
Query network usage statistics summaries.
NetworkStats.Bucket querySummaryForDevice(int networkType, String subscriberId, long startTime, long endTime)
Query network usage statistics summaries.
NetworkStats.Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, long endTime)
Query network usage statistics summaries.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

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

Added in API level 23

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 or uid. This means buckets' start and end timestamps are going to be between 'startTime' and 'endTime' parameters, state is going to be STATE_ALL and uid will vary.

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 As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId If applicable, the subscriber id of the network interface.
startTime Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime End of period. Defined in terms of "Unix time", see currentTimeMillis().
Returns
  • Statistics object or null if permissions are insufficient or error happened during statistics collection.

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

Added in API level 23

Query network usage statistics details. Only usable for uids belonging to calling user. Result is aggregated over state but not aggregated over time. This means buckets' start and end timestamps are going to be between 'startTime' and 'endTime' parameters, state is going to be STATE_ALL and uid the same as the 'uid' parameter.

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 As defined in ConnectivityManager, e.g. TYPE_MOBILE, TYPE_WIFI etc.
subscriberId If applicable, the subscriber id of the network interface.
startTime Start of period. Defined in terms of "Unix time", see currentTimeMillis().
endTime End of period. Defined in terms of "Unix time", see currentTimeMillis().
uid UID of app
Returns
  • Statistics object or null if permissions are insufficient or error happened during statistics collection.

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

Added in API level 23

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 or uid. This means buckets' start and end timestamps are going to be the same as the 'startTime' and 'endTime' parameters, state and uid are going to vary.

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

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

Added in API level 23

Query network usage statistics summaries. Result is summarised data usage for the whole device. 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 STATE_ALL and uid UID_ALL.

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

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

Added in API level 23

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 STATE_ALL and uid UID_ALL.

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