I'm writing a part of an app that displays mobile data usage and I also want it to display data from before the user installed the app. Currently, whenever I query data for a time range before the app was installed on a certain device, NetworkStatsManager.queryDetails
returns a NetworkStats
object containing no buckets.
I've already managed to get correct data about mobile data usage after the installation date in the app.
The weird thing is that I've tried to delete all cache/data in the settings and uninstall and reinstall the app. After this, the app is still able to query data from before uninstalling it, but not from before the very first time I installed the app. My gut feeling is that Android does not let you acces data from before the PACKAGE_USAGE_STATS
, the ACCESS_NETWORK_STATE
or the READ_PHONE_STATE
permissions are granted by the user.
The documentation of NetworkStats and NetworkStatsManager don't seem to mention anything about data history.
The NetworkStatsManager is obtained like this:
val networkStatsManager by lazy {
context.getSystemService(Context.NETWORK_STATS_SERVICE) as NetworkStatsManager
}
and the NetworkStats object is obtained here:
mobileDataNetworkStats = networkStatsManager.queryDetails(
ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
from.time,
to.time)
And this is how I iterate over the buckets
val bucket: NetworkStats.Bucket = NetworkStats.Bucket()
mobileDataNetworkStats.getNextBucket(bucket)
var currentUsage = usagePerUid[bucket.uid]
currentUsage = when (currentUsage) {
null -> 0
else -> currentUsage
}
currentUsage += bucket.rxBytes
currentUsage += bucket.txBytes
usagePerUid[bucket.uid] = currentUsage
So is it possible to obtain buckets via queryDetails
or querySummary
from before the installation date (or from before the date that permisisons were granted by the user)?