Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the data usage on a per-application basis on Android?

I am trying to find out the data usage on Android on a per-application basis. Something like Android Data Usage Apps and Quota / Cap Monitor Widgets: never get charged extra for data or get capped again!.

I looked at Stack Overflow question How to go about detecting data usage in the Android environment.

But it's not been of much help.


ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo( mInfo ); List<RunningAppProcessInfo> listOfRunningProcess = activityManager.getRunningAppProcesses(); Log.d(TAG, "XXSize: " + listOfRunningProcess.size());  for (RunningAppProcessInfo runningAppProcessInfo : listOfRunningProcess) {      if (runningAppProcessInfo.uid > 1026)     {         Log.d(TAG, "ANS " + runningAppProcessInfo.processName +                    " Id :" + runningAppProcessInfo.pid +                    " UID: " + runningAppProcessInfo.uid);     } } 

I tried the above code as suggested by Akos Cz. However all the UIDs are numbers, unlike app_79 as you have mentioned above. Is this all right?

like image 647
yuvi Avatar asked Aug 13 '12 18:08

yuvi


People also ask

How do I check data usage on an application?

Use apps to check your data usageGlassWire is a free app that specializes in keeping track of your data usage. There is a mobile phone version for Android, and a desktop PC version for Windows. Use an Android phone? First install GlassWire for Android.

What is application data usage?

The Data usage section in Android Settings lets you view the amount of data used by each specific app. If you notice an app using more background mobile data than you'd like it to (Snapchat comes to mind), you can restrict it from accessing the Internet till you're back in a Wi-Fi covered area.


2 Answers

The following links should help you figure out how to programmatically determine the data usage per application.

  • Create a network monitor using Android's TrafficStats class

  • Android Traffic Statistics Inside

You will need to implement your code to use the TraficStats API and track the number of bytes sent/received per UID (application).

like image 177
Akos Cz Avatar answered Sep 23 '22 01:09

Akos Cz


Use this method after create a new class PackageInformationTotal.

public void getPakagesInfoUsingHashMap() {     final PackageManager pm = getPackageManager();     // get a list of installed apps.     List<ApplicationInfo> packages = pm.getInstalledApplications(0);      // loop through the list of installed packages and see if the selected     // app is in the list     for (ApplicationInfo packageInfo : packages) {         // get the UID for the selected app         UID = packageInfo.uid;         String package_name = packageInfo.packageName;         ApplicationInfo app = null;         try {             app = pm.getApplicationInfo(package_name, 0);         } catch (NameNotFoundException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         String name = (String) pm.getApplicationLabel(app);         Drawable icon = pm.getApplicationIcon(app);         // internet usage for particular app(sent and received)         double received = (double) TrafficStats.getUidRxBytes(UID)                  / (1024 * 1024);         double send = (double) TrafficStats.getUidTxBytes(UID)                 / (1024 * 1024);         double total = received + send;          if(total>0)         {             PackageInformationTotal pi=new PackageInformationTotal();             pi.name=name;             pi.packageName=package_name;             pi.icon=icon;                            pi.totalMB=String.format( "%.2f", total )+" MB";             pi.individual_mb=String.format( "%.2f", total );             totalData+=Double.parseDouble(String.format( "%.2f", total ));             dataHash.add(pi);         Log.e(name,String.format( "%.2f", total )+" MB");         }      }     Editor edit=shared.edit();     edit.putString("Total",String.format( "%.2f", totalData));     edit.commit(); } 

After that you can track all process usages in MB.

like image 27
Mahmudul Avatar answered Sep 23 '22 01:09

Mahmudul