Reputation: 138
I developed Android app which gets data usage stats of all installed apps in a background service using TrafficStats API. Earlier this week, on Samsung Galaxy S5, after software update i.e. 6.0.1, TrafficStats returns always 0 for each uid. Same app is perfectly working on HUAWEI KIW-L21 and OS 6.0.1.
I got some posts of using NetworkStatsManager class for API 23 and above, some posts refer that this requires system level permissions.
I gone through documentation of NetworkStatsManager too.
Can anyone successfully implemented NetworkStatsManager for total and app-wise data usage?
Thanks in advance if someone really assist.
Upvotes: 1
Views: 2075
Reputation: 118
So i will show both methods together that is using TrafficeStats and NetworkStatsManager
I will get UID'S all the applications installed in the phone and use both ways to the the data usage.
you can refer this for a good example https://github.com/RobertZagorski/NetworkStats Top 5 methods were taken from his code.
//when using NetworkStatsManager you need the subscriber id
private String getSubscriberId(Context context, int networkType) {
if (ConnectivityManager.TYPE_MOBILE == networkType) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSubscriberId();
}
return "";
}
// to get mobile data recived
public long getPackageRxBytesMobile(Context context,NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
networkStats.getNextBucket(bucket);
return bucket.getRxBytes();
}
// to get mobile data transmitted
public long getPackageTxBytesMobile(Context context,NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getTxBytes();
}
// to get wifi data received
public long getPackageRxBytesWifi(NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_WIFI,
"",
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getRxBytes();
}
// to get wifi data transmitted
public long getPackageTxBytesWifi(NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_WIFI,
"",
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getTxBytes();
}
// print to log all the data usage value per application
public void printAllDataUsage(){
PackageManager pm = getPackageManager();
// get all the applications in the phone
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
NetworkStatsManager networkStatsManager = (NetworkStatsManager) getApplicationContext().getSystemService(Context.NETWORK_STATS_SERVICE);
for (ApplicationInfo packageInfo : packages) {
Log.d("MYLOG", String.valueOf(packageInfo.uid));
Log.d("MYLOG", String.valueOf(packageInfo.name));
Log.d("MYLOG", String.valueOf(packageInfo.packageName));
// get data usage from trafficStats
Log.d("MYLOG", String.valueOf(TrafficStats.getUidRxBytes(packageInfo.uid)));
Log.d("MYLOG", String.valueOf(TrafficStats.getUidTxBytes(packageInfo.uid)));
// get data usage from networkStatsManager using mobile
Log.d("MYLOG", String.valueOf(getPackageRxBytesMobile(this,networkStatsManager,packageInfo.uid)));
Log.d("MYLOG", String.valueOf(getPackageTxBytesMobile(this,networkStatsManager,packageInfo.uid)));
// get data usage from networkStatsManager using wifi
Log.d("MYLOG", String.valueOf(getPackageRxBytesWifi(networkStatsManager,packageInfo.uid)));
Log.d("MYLOG", String.valueOf(getPackageTxBytesWifi(networkStatsManager,packageInfo.uid)));
}
}
Upvotes: 2