get Process Info for packageName - Android Android OS

Android examples for Android OS:Process

Description

get Process Info for packageName

Demo Code


//package com.java2s;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;
import android.text.format.Formatter;
import java.util.List;

public class Main {
    public static String getProcessInfo(Context context, String packageName) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcesses) {
            if (processInfo.processName != packageName) {
                continue;
            }//w ww .j  av  a 2 s  . c  o m


            Debug.MemoryInfo[] memoryInfo = activityManager
                    .getProcessMemoryInfo(new int[processInfo.pid]);

            long memorySize = memoryInfo[0].getTotalPss() * 1024;
            String memory = Formatter.formatFileSize(context, memorySize);

            return memory;
        }

        return "??????";
    }
}

Related Tutorials