get Running Process - Android android.app

Android examples for android.app:ActivityManager

Description

get Running Process

Demo Code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;

public class Main {

  public static long getRunningProcess(Context context) {

    int sdkInt = android.os.Build.VERSION.SDK_INT;
    if (sdkInt >= 16) {
      ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
      MemoryInfo outInfo = new MemoryInfo();
      am.getMemoryInfo(outInfo);//  ww  w . java2 s.c o m
      return outInfo.totalMem;
    } else {
      return getRunningProcess();
    }
  }

  public static long getRunningProcess() {

    File file = new File("/proc/meminfo");
    BufferedReader bufferedReader = null;
    String total = null;
    try {
      bufferedReader = new BufferedReader(new FileReader(file));
      String line = bufferedReader.readLine();
      char[] charArray = line.toCharArray();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < charArray.length; i++) {
        char c = charArray[i];
        if (c >= '0' && c <= '9') {
          sb.append(c);
        }
      }
      total = sb.toString();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    long number = Long.parseLong(total) * 1024;
    return number;
  }

}

Related Tutorials