如何获取android正在运行的进程

如何获取android正在运行的进程,第1张

ActivityManagerRunningAppProcessInfo类说明: 封装了正在运行的进程信息常用字段:int pid 进程IDint uid 进程所在的用户IDString processName 进程名,默认是包名或者由android:process=””属性指定String [ ] pkgList 运行在该进程下的所有应用程序包名关于ActivityManagerRunningAppProcessInfo更多信息,请查看<Android中应用程序的信息---ActivityManager的使用>PackageManger类说明: 封装了对应用程序信息的 *** 作获得应用程序信息的的方法如下:public abstract ApplicationInfo getApplicationInfo(String packageName, int flags)参数:packagename 包名flags 该ApplicationInfo是此flags标记,通常可以直接赋予常数0即可功能:返回ApplicationInfo对象

第1种方法:

在命令行输入:

adb shell

然后再#号后边输入:

logcat :S ActivityManager:V    

如图:红框的内容,/前半部分是包名,/后半部分是当前的activity名称

第2种方法:

命令窗口输入:adb shell logcat | grep cmp=

然后打开想要获取的APP,终端会显示当前打开的APP包名和Activity名称,如下图:

ActivityManager mActivityManager = (ActivityManager)

this

getSystemService(ACTIVITY_SERVICE);

List<ActivityManagerRunningAppProcessInfo>

mRunningProcess = mActivityManager

getRunningAppProcesses();

int i =

1;

for (ActivityManagerRunningAppProcessInfo amProcess :

mRunningProcess)

{

Logi("Application", (i++) + "PID: " +

amProcesspid

+ "(processName=" + amProcessprocessName +

"UID="+amProcessuid+")");

}

 Device Year Class 的主要功能是根据 CPU核数、时钟频率 以及 内存大小 对设备进行分级。代码很简单,只包含两个类:DeviceInfo-> 获取设备参数,YearClass-> 根据参数进行分级。下表是 Facebook 公司提供的分级标准,其中Year栏表示分级结果。Year Cores Clock RAM2008 1 528MHz 192MB2009 n/a 600MHz 290MB2010 n/a 10GHz 512MB2011 2 12GHz 1GB2012 4 15GHz 15GB2013 n/a 20GHz 2GB2014 n/a >2GHz >2GB关于输出年份的计算方法可以参考源码,本文只把一些比较常用的功能抽取出来做一个简要介绍。获取 CPU 核数我们都知道,Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。Android 的 CPU 设备文件位于/sys/devices/system/cpu/目录,文件名的的格式为cpu\d+。12345678910root@generic_x86_64:/sys/devices/system/cpu # ls <b>cpu0</b> cpufreqcpuidlekernel_maxmodaliasofflineonlinepossiblepowerpresentuevent统计一下文件个数便可以获得 CPU 核数。1234567891011121314151617181920212223242526272829303132333435public static int getNumberOfCPUCores() {if (BuildVERSIONSDK_INT <= BuildVERSION_CODESGINGERBREAD_MR1) {// Gingerbread doesn't support giving a single application access to both cores, but a// handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core// chipset and Gingerbread; that can let an app in the background run without impacting// the foreground application But for our purposes, it makes them single corereturn 1;}int cores;try {cores = new File("/sys/devices/system/cpu/")listFiles(CPU_FILTER)length;} catch (SecurityException e) {cores = DEVICEINFO_UNKNOWN;} catch (NullPointerException e) {cores = DEVICEINFO_UNKNOWN;}return cores;}private static final FileFilter CPU_FILTER = new FileFilter() {@Overridepublic boolean accept(File pathname) {String path = pathnamegetName();//regex is slow, so checking char by charif (pathstartsWith("cpu")) {for (int i = 3; i < pathlength(); i++) {if (pathcharAt(i) < '0' pathcharAt(i) > '9') {return false;}}return true;}return false;}};回到顶部获取时钟频率获取时钟频率需要读取系统文件 -/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq或者/proc/cpuinfo。我的 Android 模拟器中并没有cpuinfo_max_freq文件,因此只能读取/proc/cpuinfo。/proc/cpuinfo包含了很多 cpu 数据。processor : 0vendor_id : GenuineIntelcpu family : 6model : 70model name : Intel(R) Core(TM) i7-4770HQ CPU @ 220GHzstepping : 1cpu MHz : 0000cache size : 1024 KBfdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 4wp : yes代码如下:public static int getCPUMaxFreqKHz() {int maxFreq = DEVICEINFO_UNKNOWN;try {for (int i = 0; i < getNumberOfCPUCores(); i++) {String filename ="/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";File cpuInfoMaxFreqFile = new File(filename);if (cpuInfoMaxFreqFileexists()) {byte[] buffer = new byte[128];FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);try {streamread(buffer);int endIndex = 0;//Trim the first number out of the byte bufferwhile (buffer[endIndex] >= '0' && buffer[endIndex] <= '9'&& endIndex < bufferlength) endIndex++;String str = new String(buffer, 0, endIndex);Integer freqBound = IntegerparseInt(str);if (freqBound > maxFreq) maxFreq = freqBound;} catch (NumberFormatException e) {//Fall through and use /proc/cpuinfo} finally {streamclose();}}}if (maxFreq == DEVICEINFO_UNKNOWN) {FileInputStream stream = new FileInputStream("/proc/cpuinfo");try {int freqBound = parseFileForValue("cpu MHz", stream);freqBound = 1000; //MHz -> kHzif (freqBound > maxFreq) maxFreq = freqBound;} finally {streamclose();}}} catch (IOException e) {maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown}return maxFreq;}回到顶部获取内存大小如果 SDK 版本大于等于JELLY_BEAN,可以通过ActivityManager来获取内从大小。 ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);amgetMemoryInfo(memInfo);如果版本低于JELLY_BEAN,则只能读取系统文件了。FileInputStream stream = new FileInputStream("/proc/meminfo");totalMem = parseFileForValue("MemTotal", stream);完整代码如下: @TargetApi(BuildVERSION_CODESJELLY_BEAN)public static long getTotalMemory(Context c) {// memInfototalMem not supported in pre-Jelly Bean APIsif (BuildVERSIONSDK_INT >= BuildVERSION_CODESJELLY_BEAN) {ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);amgetMemoryInfo(memInfo);if (memInfo != null) {return memInfototalMem;} else {return DEVICEINFO_UNKNOWN;}} else {long totalMem = DEVICEINFO_UNKNOWN;try {FileInputStream stream = new FileInputStream("/proc/meminfo");try {totalMem = parseFileForValue("MemTotal", stream);totalMem = 1024;} finally {streamclose();}} catch (IOException e) {}return totalMem;}}

如何获取android 进程信息,有需要的朋友可以参考下。

有时候为了完成某些功能比如监测某些程序的运行情况,我们可以通过动态的获取android 进程信息:

1)首先我们定义下进程信息的model:

public class ProcessInfo {

private int pid; // 进程id

private int uid; // 进程所在的用户id

private int memSize; //进程占用的内存,kb

private String processName; // 进程名

public String pkgnameList[] ;//运行在进程里的对应的所有程序的包名

public int getPid() {

return thispid;

}

public void setPid(int pid) {

thispid = pid;

}

public int getUid() {

return thisuid;

}

public void setUid(int uid) {

thisuid = uid;

}

public int getMemSize() {

return thismemSize;

}

public void setMemSize(int memSize) {

thismemSize = memSize;

}

public String getProcessName() {

return thisprocessName;

}

public void setPocessName(String processName) {

thisprocessName = processName;

}

}

其次,我们通过android API 获取进程信息:

// 获得系统进程信息

private ArrayList<ProcessInfo> getRunningAppProcessInfo(){

ActivityManager mActivityManager = (ActivityManager) getSystemService(ContextACTIVITY_SERVICE);

ArrayList<ProcessInfo> processInfoList = new ArrayList<ProcessInfo>();

List<ActivityManagerRunningAppProcessInfo> appProcessList = mActivityManagergetRunningAppProcesses();

for (ActivityManagerRunningAppProcessInfo appProcessInfo : appProcessList) {

int pid = appProcessInfopid;

int uid = appProcessInfouid;

String processName = appProcessInfoprocessName;

int[] myMempid = new int[] { pid };

DebugMemoryInfo[] memoryInfo = mActivityManagergegetProcessMemoryInfo(myMempid);

//kb

int memSize = memoryInfo[0]dalvikPrivateDirty;

ProcessInfo processInfo = new ProcessInfo();

processInfosetPid(pid);

processInfosetUid(uid);

processInfosetMemSize(memSize);

processInfosetPocessName(processName);

processInfopkgnameList = appProcessInfopkgList ;

processInfoListadd(processInfo);

return processInfoList;

}

//判断某进程是否存在

private boolean isProcessExisting(String packageName){

ArrayList<ProcessInfo> processInfoList = getRunningAppProcessInfo();

for(ProcessInfo process : processInfoList){

String[] packageList = processpkgnameList;

for (String pkg : packageList) {

if(pkgequals(packageName)){

return true;

}

}

}

return false;

}

在Android中取得当前进程名

说明:上面代码关键的两个地方解释,也就是取得当前进程名

androidosProcessmyPid()) //获取PID,目前获取自己的也只有该API

ActivityManagerRunningAppProcessInfo类

说明: 封装了正在运行的进程信息

常用字段:

int   pid    进程ID

int   uid    进程所在的用户ID

String   processName 进程名,默认是包名或者由Android:process=””属性指定

String [ ]   pkgList      运行在该进程下的所有应用程序包名

以上就是关于如何获取android正在运行的进程全部的内容,包括:如何获取android正在运行的进程、adb 获取app包名和当前的activity名称、如何获取Android正在运行的应用和它的pid等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9274515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存