Android Display Running Application Process Programmatically – Example

In this post we will see how to display all the running application process in a device or emulator.  It can be done by creating an instance for the ActivityManager Class . In this sample application we will get the list of running app process in a List and will display it in a TextView .


So lets start:


1.Create a new project File ->New -> Project ->Android ->Android Application Project. While creating the new project, name the activity as MainActivity(MainActivity.java)and layout as activity_main.xml.

2.Now let us design the UI for the MainActivity i.e activity_main.xml with a TextView.

activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Running Apps \n" />

</RelativeLayout>


3.Now in the MainActivity use ActvityManager instance and get the appropriate system service and list them in a textview.

MainActivity.java:

package com.example.piolt;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  TextView tv = (TextView) findViewById(R.id.textView);

  ActivityManager activityManager = (ActivityManager) this
    .getSystemService(ACTIVITY_SERVICE);

  List<RunningAppProcessInfo> procInfos = activityManager
    .getRunningAppProcesses();
  for (int idx = 0; idx < procInfos.size(); idx++) {

   tv.setText(tv.getText() + "" + (idx + 1) + "."
     + procInfos.get(idx).processName + "\n");

  }

 }

}

4.Run the project by rightclicking project Run as android project in an android device.


Output:



The output of this example would be similar to the one as follows: