Getting Mobile Data Usage History Using NetworkStatsManager

Getting Mobile Data Usage History Using NetworkStatsManager

The NetworkStatsManager class in Android provides an API to query network data usage statistics. This article will guide you through the process of obtaining mobile data usage history using this class.

Understanding NetworkStatsManager

  • NetworkStatsManager: A system service for collecting and retrieving statistics on network data usage.
  • Usage: Provides information about data usage per network (Wi-Fi, cellular), application, and time interval.
  • Permissions: Requires the ACCESS_NETWORK_STATE and PACKAGE_USAGE_STATS permissions.

Retrieving Mobile Data Usage History

Here’s how to obtain mobile data usage history for a specific period using NetworkStatsManager:

1. Access NetworkStatsManager


NetworkStatsManager networkStatsManager = (NetworkStatsManager) getSystemService(Context.NETWORK_STATS_SERVICE);

2. Define Time Interval


long startTime = System.currentTimeMillis() - (24 * 60 * 60 * 1000); // Yesterday
long endTime = System.currentTimeMillis();

3. Query Data Usage


NetworkStats.Bucket bucket = networkStatsManager.querySummaryForDevice(NetworkStats.Bucket.BUCKET_WIFI, startTime, endTime);

This query retrieves summary information for the WiFi network within the specified time interval. You can replace NetworkStats.Bucket.BUCKET_WIFI with NetworkStats.Bucket.BUCKET_MOBILE for cellular data usage.

4. Process the Results


if (bucket != null) {
    long totalRxBytes = bucket.getRxBytes();
    long totalTxBytes = bucket.getTxBytes();

    Log.d("NetworkStats", "Total received bytes: " + totalRxBytes);
    Log.d("NetworkStats", "Total transmitted bytes: " + totalTxBytes);
}

Example Usage

Here’s a complete code example demonstrating how to retrieve and display mobile data usage:


import android.content.Context;
import android.net.NetworkStats;
import android.net.NetworkStatsManager;
import android.util.Log;

public class MobileDataUsage {

    public static void main(String[] args) {
        // Replace with your application context
        Context context = getApplicationContext(); 

        NetworkStatsManager networkStatsManager = (NetworkStatsManager) context.getSystemService(Context.NETWORK_STATS_SERVICE);

        // Specify the time interval
        long startTime = System.currentTimeMillis() - (24 * 60 * 60 * 1000); // Yesterday
        long endTime = System.currentTimeMillis();

        // Query mobile data usage
        NetworkStats.Bucket bucket = networkStatsManager.querySummaryForDevice(NetworkStats.Bucket.BUCKET_MOBILE, startTime, endTime);

        if (bucket != null) {
            long totalRxBytes = bucket.getRxBytes();
            long totalTxBytes = bucket.getTxBytes();

            Log.d("NetworkStats", "Total received bytes: " + totalRxBytes);
            Log.d("NetworkStats", "Total transmitted bytes: " + totalTxBytes);
        }
    }
}

Output


D/NetworkStats: Total received bytes: 123456789
D/NetworkStats: Total transmitted bytes: 987654321

Notes

  • Requires ACCESS_NETWORK_STATE and PACKAGE_USAGE_STATS permissions.
  • The returned data is in bytes.
  • You can adjust the startTime and endTime values to retrieve data for different periods.
  • For more granular data usage, refer to the queryDetailsForUid() method in NetworkStatsManager.


Leave a Reply

Your email address will not be published. Required fields are marked *