安卓(Android) 怎么获取正在运行的进程

2019年10月13日14:37:53安卓(Android) 怎么获取正在运行的进程已关闭评论 244 1186字阅读3分57秒

1.简介

安卓(Android) 怎么获取正在运行的进程?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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);
            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) {
            
            e.printStackTrace();
        }
        long number = Long.parseLong(total) * 1024;
        return number;
    }
}

 
继续阅读
编程十万个怎么办