Using TrafficStats i was checking the youtube app data usage.In some devices it is working fine but not with many other devices. I found that from developer site, These statistics may not be available on all platforms. If the statistics are not supported by this device, UNSUPPORTED will be returned.
So in these case how can I get the device app usage ?
I was using TrafficStats.getUidRxBytes(packageInfo.uid) + TrafficStats.getUidTxBytes(packageInfo.uid);
this is returning -1 everytime.
We can use NetworkStats. https://developer.android.com/reference/android/app/usage/NetworkStats.html Please see a sample repo which I got the clue. https://github.com/RobertZagorski/NetworkStats We can see a similar stackoverflow question as well. Getting mobile data usage history using NetworkStatsManager
Then I needed to modify this logic for some particular devices. In these devices the normal method won't return proper usage values. So I modified is as
/* getting youtube usage for both mobile and wifi. */