TrafficStats Api android and calculating daily data usage

There are confusions in the following two TrafficStats methods for Android: getUidTxBytes (int uid) and getUidRxBytes (int uid) . These two methods return the number of bytes sent and received over the network for this UID. But what is the unit of time, is it per second. If I want to calculate the data sent and received per day per application, what should I do. I am trying to do one way to store data in sql and keep adding data to the table. Is it correct.

+8
android networking
source share
4 answers

These are counters “since the interface passed” or “since the application was launched with this UID”. Tell me, if your phone goes into "Airplane mode" and then back, the counters can start from scratch again. If you need values ​​per second, you will need to call these functions every second, and then use the delta from the last call. If the delta is negative, just use the as-is value, it means the counter has started from scratch again.

One more thing: as far as I know, these counters only count TCP / IP. Not UDP. Therefore, if you need very accurate accounting, and the application in question uses UDP / IP or any protocol other than TCP, these counters will be erroneous.

To understand how this feature works, look at the Android source, freely available. The file in question is ./frameworks/base/core/jni/android_net_TrafficStats.cpp

This function gets data from /proc/uid_stat/[uid]/tcp_snd . If you need more information about this, you will need to dive into the Linux kernel ...

+5
source share

These counters contain the number of bytes since the last reboot. On some phones, these counters may periodically reset, but most of the time they only reset after a reboot. Switching to airplane mode or switching between mobile and Wi-Fi will not reset these counters.

The important point is that these counters do not include packet overhead, but only the size of the payload. Thus, this usually means that 3-4% of the data may be unaccounted for. However, if it is a streaming, torrent, or VoIP application where packet loads are small, there may be a much larger amount of unaccounted data.

Interestingly, getTotalRxBytes (received bytes for all interfaces, for example, for mobile and Wi-Fi) and getMobileRxBytes (received bytes on the mobile interface only) include all bytes, including service ones. Thus, the total number of bytes of your byte will be less than the total number of bytes of your interface and therefore less than the amount of data your network operator bills you for.

In the latter case, most streaming applications do not account for their data under their own UID. They are counted under the identifier system.media. Therefore, if you control the use of data for YouTube, only a very small amount of data will be displayed in this application; the rest will be under the media UID (1013).

+4
source share

Here I get those applications that have an Internet permission, you can change the name of the Permission and get the application as you wish.

 ArrayList<AppObject> listApps; public void getAllAppList() { listApps = new ArrayList<AppObject>(); PackageManager p = getPackageManager(); List<ApplicationInfo> packages = p.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo applicationInfo : packages) { try { PackageInfo packageInfo = p.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS); String[] permissions = packageInfo.requestedPermissions; for (String permissionName : permissions) { if (permissionName.equals("android.permission.INTERNET")) { ApplicationInfo appInfo = packageInfo.applicationInfo; AppObject appObject = new AppObject(); appObject.appDrawable = getPackageManager().getApplicationIcon(appInfo); appObject.appName = (String) getPackageManager().getApplicationLabel(appInfo); appObject.dataUsage = getDataUsage(appInfo); listApps.add(appObject); } } } catch (NullPointerException e) { e.printStackTrace(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } Debug.e("APP_SIZE", ":" + listApps.size()); appsAdapter.addAll(listApps); } public String getDataUsage(ApplicationInfo appInfo) { int uid = appInfo.uid; double received = (double) TrafficStats.getUidRxBytes(uid) / (1024 * 1024); double sent = (double) TrafficStats.getUidTxBytes(uid) / (1024 * 1024); double total = received + sent; return String.format("%.2f", total) + " MB"; } 
+2
source share

getUidRxBytes () and getUidTxBytes () are mainly used for received and transmitted bytes, respectively. To track your data for each application, just find the uid of each process and find the corresponding data for each process, and this will be your data for each application, and then you can use this code for calculations.

  TextView totData = (TextView)findViewById(R.id.totData); TextView wifiTot = (TextView)findViewById(R.id.wifitotData); TextView wifiTX = (TextView)findViewById(R.id.wifiUpData); TextView wifiRX = (TextView)findViewById(R.id.wifiDownData); TextView mobileTot = (TextView)findViewById(R.id.mobtotData); TextView mobTX = (TextView)findViewById(R.id.mobUpData); TextView mobRX = (TextView)findViewById(R.id.mobDownData); /* * Converting bytes to MB */ long rxBytes = TrafficStats.getTotalRxBytes()/1048576; long txBytes = TrafficStats.getTotalTxBytes()/1048576; long mobUpload = TrafficStats.getMobileTxBytes()/1048576; long mobDown = TrafficStats.getMobileRxBytes()/1048576; long wifiUpload = txBytes-(mobUpload); long wifiDown = rxBytes-(mobDown); wifiRX.setText(Long.toString(wifiDown)); wifiTX.setText(Long.toString(wifiUpload)); long wifitot = wifiUpload+wifiDown; wifiTot.setText(Long.toString(wifitot)); mobTX.setText(Long.toString(mobUpload)); mobRX.setText(Long.toString(mobDown)); long mobTot = mobUpload+mobDown; mobileTot.setText(Long.toString(mobTot)); totData.setText(Long.toString(wifitot+mobTot)); 
0
source share

All Articles