How to get running process list and traffic statistics
Today we got little application which displays list of working applications on Android in a ListView. When a process name is clicked application displays information about amount of data that application used – traffic info. We present following topics here:
- Display data in a form of ListView
- Display process list and their traffic statistics
Display data in a form of ListView
We want an icon and text to display in a ListView. Our layout looks like this.
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000"> <ImageView android:id="@+id/detailsIco" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_action_search" /> <TextView android:id="@+id/appNameText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="APP NAME GOES HERE"> </TextView> </LinearLayout>
Next we need a ListAdapter which is responsible for building a list raw.
ListAdapter.java
package com.itcuties.apps.TransferUsageApp.adapters; import java.util.List; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import com.itcuties.apps.TransferUsageApp.R; public class ListAdapter extends ArrayAdapter<RunningAppProcessInfo> { // List context private final Context context; // List values private final List<RunningAppProcessInfo> values; public ListAdapter(Context context, List<RunningAppProcessInfo> values) { super(context, R.layout.main, values); this.context = context; this.values = values; } /** * Constructing list element view */ @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.main, parent, false); TextView appName = (TextView) rowView.findViewById(R.id.appNameText); appName.setText(values.get(position).processName); return rowView; } }
Display process list and their traffic statistics
Now we are ready to display processes and traffic information. Since we use ListView and which is responsive our activity will extend ListActivity to add functionality of performing action when an item is clicked – we override onListItemClick method.
MainActivity.java
package com.itcuties.apps.TransferUsageApp; import java.util.List; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.app.ListActivity; import android.net.TrafficStats; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.itcuties.apps.TransferUsageApp.adapters.ListAdapter; public class MainActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get running processes ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses(); if (runningProcesses != null && runningProcesses.size() > 0) { // Set data to the list adapter setListAdapter(new ListAdapter(this, runningProcesses)); } else { // In case there are no processes running (not a chance :)) Toast.makeText(getApplicationContext(), "No application is running", Toast.LENGTH_LONG).show(); } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { long send = 0; long recived = 0; // Get UID of the selected process int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid; // Get traffic data recived = TrafficStats.getUidRxBytes(uid); send = TrafficStats.getUidTxBytes(uid); // Display data Toast.makeText(getApplicationContext(), "UID " + uid + " details...\n send: " + send/1000 + "kB" + " \n recived: " + recived/1000 + "kB", Toast.LENGTH_LONG).show(); } }
Full code of this application is available here.
I believe I am in the right environment. Its time to config and deploy…
yeah! go for it man! :)
How can I hide the system processes, like System UI or Package Access Helper?
Sorry for my English :)
Hi D25,
if you want to hide the system processes from the list just remove everything that has the name starting with
com.android
andsystem
from the runningProcesses.RunningAppProcessInfo.processName
contains the name of the process.Take care,
itcuties
how we can get the battery consumption information by an app can u give me some sample example for it or solution thnx
Hi Jitendra,
We’ve been googling for a while to check how to answer your question. It seems that Android doesn’t give a clean solution for getting battery usage information per process.
Here are some useful links
http://stackoverflow.com/questions/6051807/per-process-power-consumption-in-android
http://stackoverflow.com/questions/5152447/android-battery-usage-profiling
and
http://android.stackexchange.com/questions/20090/how-does-android-calculate-battery-consumption-by-apps
This is a hard one :) We can’t think of a solution right now.
Please let us know if you have something.
Cheers,
itcuties
HI. excelent post, everything worked just fine. Though I have a doubt. What is the meaning of the data you show when clicking the process, KBs sent and KBs received?
Thanks i advance.
Hi ElKsa,
we are displaying sent and received kBs.
Take care,
itcuties
How Do I close a process of this list ?
and How Do I get the cache for each App?
Hello,
According to the documentation this should do the trick (place this code in the
MainActivity. onListItemClick
):But it seems that the only application that you can close is the one that calls this code. Do you maybe know why?
What do you mean by getting App’s cache?
itcuties
Ok, case solved :)
This code, when put in the
MainActivity.onListItemClick
kills the processTo use it you need this permission in your
AndroidManifest.xml
filehey ,
When i run this code on any phone , not only this code. But other samples across the net. I always get 0 rec and 0 sent for all the apps.
Anyone know whats the problem?
This makes total sense . But i have query , how can i print all the process running from one application ? i.e if my application contains lot process which is included in manifest file . like below :
and so on …..
So , is it possible to print these services also ?? With above code, it will just print the main process but not the services which are added in manifest file .
Note : These service start as soon as my application is launched (verified by adb shell ps)
Kindly help
When adding xml to the comment please use [xml]YOUR XML HERE[/xml].
i am sorry , here is my missing xml from above comment
and so on …..
can someone pls help me how can i find memory usage for each process how much memory is consumed by each process by passing pid or package name to the methods and also what is the meaning of active processes or deactive process ?
Hello, i want to show the traffic stats constantly in front of everylist item without showing it in to toast ,plz help me with the code changing i am beginner
How can I get the traffic of the portal hotspot I shared on my android phone? I have got the UID of com.huawei.wifihotspot is 10047,Is it the process of portal hotspot ? If the answer is true,why the send and receive is always 0.Even I have used other phone connected this phone’s hotspot shared for a while and have scaned some news on browser.
Sorry for my English
Above code is not working for lollipop and above. Please suggest