Friday, March 7, 2014

Android : Fetch all running process ,memory and Process ID

Fetch all running process ,memory and Process ID


import java.util.List;
//import android.R;
import android.os.Bundle;
import android.os.Debug;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
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();
         // List<RunningAppProcessInfo> procInfos2=activityManager.
         for (int idx = 0; idx < procInfos.size(); idx++)
         {
            tv.setText(tv.getText() + "" + (idx + 1) + "."  + procInfos.get(idx).processName +"(" +procInfos.get(idx).pid+")"+"\n");
         }

        
         ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
         List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();

         int[] pids = new int[procsInfo.size()];
         for (int i = 0; i < procsInfo.size(); i++) {
             ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
             pids[i] = info.pid;
         }

         Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
         for (int idx = 0; idx < procsMemInfo.length; idx++)
         {
            tv.setText(tv.getText() + "" + (idx + 1) + "."  +procsMemInfo[idx].getTotalPss()+ "kb\n");
         }
    
}}

 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>

No comments:

Post a Comment