google可能为了安全考虑,在5.1.+后调用activitymanager.getRunningAppProcesses()方法只能返回你自己应用的进程,那如何在5.1.+后获取运行中进程呢?一个大神stackoverflow给出了答案(点击跳转)。如果你能熟练的导入第三方库,那么相信你可以不用向下看了,如果你选择向下看,那我会用白话文教你一步步实现。首先到这位答主的github上下载他上传的开源库,梯子自备(点击跳转)。没有梯子可以到我的个人百度云下载:链接: http://pan.baidu.com/s/1kVjjPsF 密码: ag6u

下载完成后解压目录结构

我们只需要其中的红线框住的部分,但是直接导入,肯定会处很多的问题,我们先来处理一部分,打开libsuperuser
其中你框选的三个文件是我们要注意的。如果要导入的文件中有build.gradle或AndroidManifest.xml、project.properties文件,需要将其用记事本打开后,将里面的gradle及Android版本修改为自己使用的,如果不知道的话可以新建一个工程或者打开以前建的工程中的相关文件进行对比查看。

修改完成后来到android studio的project视图下将他粘贴进去

粘贴进去肯定会叫你同步,然后就同步罗,会发现一些问题,其中常见的是提示:
Error:(2, 0) Plugin with id ‘com.github.dcendents.android-maven’ not found
Error:(2, 0) Plugin with id ‘com.jfrog.bintray.gradle’ not found
这是因为有两个插件我们没有装上,我们来到Project下,在那个build.grade里面添加全局依赖

将这两个依赖的插件写上,建议写一个同步一次分两次进行,第二次下载需要比较长的时间

       //自动化maven打包插件classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
       //自动上传至bintray插件classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0"

填写好后我们会发现没有编译错误了,那我们要怎么去用添加的这个开源库呢??来到android视图下的build.gradle(Module.app)下添加依赖

 compile 'eu.chainfire:libsuperuser:1.0.0.+'

重新同步后,我们就可以调用里面的方法了,新建一个ProgressManager类

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;import java.util.ArrayList;
import java.util.List;import eu.chainfire.libsuperuser.Shell;/*** @author Jared Rummler*/
public class ProcessManager {private static final String TAG = "ProcessManager";private static final String APP_ID_PATTERN;static {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {// Android 4.2 (JB-MR1) changed the UID name of apps for multiple user account support.APP_ID_PATTERN = "u\\d+_a\\d+";} else {APP_ID_PATTERN = "app_\\d+";}}public static List<Process> getRunningProcesses() {List<Process> processes = new ArrayList<>();List<String> stdout = Shell.SH.run("toolbox ps -p -P -x -c");for (String line : stdout) {try {processes.add(new Process(line));} catch (Exception e) {android.util.Log.d(TAG, "Failed parsing line " + line);}}return processes;}public static List<Process> getRunningApps() {List<Process> processes = new ArrayList<>();List<String> stdout = Shell.SH.run("toolbox ps -p -P -x -c");int myPid = android.os.Process.myPid();for (String line : stdout) {try {Process process = new Process(line);if (process.user.matches(APP_ID_PATTERN)) {if (process.ppid == myPid || process.name.equals("toolbox")) {// skip the processes we created to get the running apps.continue;}processes.add(process);}} catch (Exception e) {android.util.Log.d(TAG, "Failed parsing line " + line);}}return processes;}public static class Process implements Parcelable {/** User name */public final String user;/** User ID */public final int uid;/** Processes ID */public final int pid;/** Parent processes ID */public final int ppid;/** virtual memory size of the process in KiB (1024-byte units). */public final long vsize;/** resident set size, the non-swapped physical memory that a task has used (in kiloBytes). */public final long rss;public final int cpu;/** The priority */public final int priority;/** The priority, <a href="https://en.wikipedia.org/wiki/Nice_(Unix)">niceness</a> level */public final int niceness;/** Real time priority */public final int realTimePriority;/** 0 (sched_other), 1 (sched_fifo), and 2 (sched_rr). */public final int schedulingPolicy;/** The scheduling policy. Either "bg", "fg", "un", "er", or "" */public final String policy;/** address of the kernel function where the process is sleeping */public final String wchan;public final String pc;/*** Possible states:* <p/>* "D" uninterruptible sleep (usually IO)* <p/>* "R" running or runnable (on run queue)* <p/>* "S" interruptible sleep (waiting for an event to complete)* <p/>* "T" stopped, either by a job control signal or because it is being traced* <p/>* "W" paging (not valid since the 2.6.xx kernel)* <p/>* "X" dead (should never be seen)* </p>* "Z" defunct ("zombie") process, terminated but not reaped by its parent*/public final String state;/** The process name */public final String name;/** user time in milliseconds */public final long userTime;/** system time in milliseconds */public final long systemTime;// Much dirty. Much ugly.private Process(String line) throws Exception {String[] fields = line.split("\\s+");user = fields[0];uid = android.os.Process.getUidForName(user);pid = Integer.parseInt(fields[1]);ppid = Integer.parseInt(fields[2]);vsize = Integer.parseInt(fields[3]) * 1024;rss = Integer.parseInt(fields[4]) * 1024;cpu = Integer.parseInt(fields[5]);priority = Integer.parseInt(fields[6]);niceness = Integer.parseInt(fields[7]);realTimePriority = Integer.parseInt(fields[8]);schedulingPolicy = Integer.parseInt(fields[9]);if (fields.length == 16) {policy = "";wchan = fields[10];pc = fields[11];state = fields[12];name = fields[13];userTime = Integer.parseInt(fields[14].split(":")[1].replace(",", "")) * 1000;systemTime = Integer.parseInt(fields[15].split(":")[1].replace(")", "")) * 1000;} else {policy = fields[10];wchan = fields[11];pc = fields[12];state = fields[13];name = fields[14];userTime = Integer.parseInt(fields[15].split(":")[1].replace(",", "")) * 1000;systemTime = Integer.parseInt(fields[16].split(":")[1].replace(")", "")) * 1000;}}private Process(Parcel in) {user = in.readString();uid = in.readInt();pid = in.readInt();ppid = in.readInt();vsize = in.readLong();rss = in.readLong();cpu = in.readInt();priority = in.readInt();niceness = in.readInt();realTimePriority = in.readInt();schedulingPolicy = in.readInt();policy = in.readString();wchan = in.readString();pc = in.readString();state = in.readString();name = in.readString();userTime = in.readLong();systemTime = in.readLong();}public String getPackageName() {if (!user.matches(APP_ID_PATTERN)) {// this process is not an applicationreturn null;} else if (name.contains(":")) {// background service running in another process than the main app processreturn name.split(":")[0];}return name;}public PackageInfo getPackageInfo(Context context, int flags)throws PackageManager.NameNotFoundException{String packageName = getPackageName();if (packageName == null) {throw new PackageManager.NameNotFoundException(name + " is not an application process");}return context.getPackageManager().getPackageInfo(packageName, flags);}public ApplicationInfo getApplicationInfo(Context context, int flags)throws PackageManager.NameNotFoundException{String packageName = getPackageName();if (packageName == null) {throw new PackageManager.NameNotFoundException(name + " is not an application process");}return context.getPackageManager().getApplicationInfo(packageName, flags);}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(user);dest.writeInt(uid);dest.writeInt(pid);dest.writeInt(ppid);dest.writeLong(vsize);dest.writeLong(rss);dest.writeInt(cpu);dest.writeInt(priority);dest.writeInt(niceness);dest.writeInt(realTimePriority);dest.writeInt(schedulingPolicy);dest.writeString(policy);dest.writeString(wchan);dest.writeString(pc);dest.writeString(state);dest.writeString(name);dest.writeLong(userTime);dest.writeLong(systemTime);}public static final Creator<Process> CREATOR = new Creator<Process>() {public Process createFromParcel(Parcel source) {return new Process(source);}public Process[] newArray(int size) {return new Process[size];}};}}

通过这个类我们就可以获得运行中的进程了

//获取运行中进程List<ProcessManager.Process> runningProcesses = ProcessManager.getRunningProcesses();

但是有很多进程都没有名字,如果你想让用户直观的管理进程的话可能需要下面这个循环过滤没有名字的线程

 List<ProcessManager.Process> Processes=new ArrayList<>();PackageManager pm = getPackageManager();runningProcesses = ProcessManager.getRunningProcesses();for (ProcessManager.Process runningProcesse : runningProcesses) {String packname = runningProcesse.getPackageName();try {ApplicationInfo applicationInfo = pm.getApplicationInfo(packname, 0);} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();continue;}Processes.add(runningProcesse);}

我的博客网站:http://huyuxin.top/欢迎大家访问!评论!

Android5.1.+ getRunningAppProcesses()获取运行中进程(第三方开源库)相关推荐

  1. (转)CocoaPods:管理Objective-c 程序中各种第三方开源库关联

    在我们的iOS程序中,经常会用到多个第三方的开源库,通常做法是去下载最新版本的开源库,然后拖拽到工程中. 但是,第三方开源库的数量一旦比较多,版本的管理就非常的麻烦.有没有什么办法可以简化对第三方库的 ...

  2. python多进程调试_使用pyrasite进行python进程调试,改变运行中进程的代码

    后端开发中有时会遇到这种情况:进程运行中偶现,重启进程问题就消失:或者是,进程一定要运行一段时间才会出现问题:又或是,极难复现的问题出现了,然而已有的log不足以定位 对于这些情况,尽管大部分时候,我 ...

  3. 获取运行中的Word,excel对象

    参考 获取运行中的Word对象:https://blog.csdn.net/seker_2006/article/details/1335702 PPT在CreateDispatch _T(" ...

  4. Android NDK编译中在libs\armeabi中加入第三方so库文件的方法

    Android NDK编译中在libs\armeabi中加入第三方so库文件的方法 假设要加入库文件的名字为libffmpeg.so文件 1.要在project\jni目录下新建一目录prebuilt ...

  5. iOS开发中解决第三方静态库符号冲突的终极方案

    iOS开发中解决第三方静态库符号冲突的终极方案 背景 在iOS开发的时候,经常会使用各种第三方静态库,这些库内部可能会打包了相同的第三方库.那么在链接的时候就会发生符号冲突. 例如:A厂商提供的lib ...

  6. 基于第三方开源库的OPC服务器开发指南(2)——LightOPC的编译及部署

    基于第三方开源库的OPC服务器开发指南(2)--LightOPC的编译及部署 前文已经说过,OPC基于微软的DCOM技术,所以开发OPC服务器我们要做的事情就是开发一个基于DCOM的EXE文件.一个代 ...

  7. 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善...

    链接地址:http://www.tuicool.com/articles/jyA3MrU 时间 2015-01-05 10:08:18  我是程序猿,我为自己代言 原文  http://blog.cs ...

  8. 45.Android 第三方开源库收集整理(转)

    原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样,  ...

  9. Android常用的第三方开源库和框架

    第三方开源库和组件 一个专注于平滑滚动的Android图像加载和缓存库 https://github.com/bumptech/glide 图片缓存Universal-Image-Loader: ht ...

最新文章

  1. maven多项目打包报错---子模块相互依赖打包时所遇到的问题:依赖的程序包找不到 package xxx does not exist
  2. C 语言编程 — 基本数据类型
  3. 北京超前布局通用人工智能 我国首个超大规模智能模型系统发布
  4. ttf能改成gfont吗_如何编辑ttf字体文件
  5. Linux 零拷贝 sendfile函数中文说明及实际操作
  6. 小程序 - 参考数据 - ASC字符码表和常用的中文字符编码表
  7. GDB的工作原理及skyeye远程调试
  8. xtrabackup启动过程中出现的报错
  9. 大数据如何应用在企业人力资源管理
  10. Instagram技术透析:Mike Krieger, Instagram at the Airbnb tech talk, on Scaling Instagram
  11. css居中的几种方法_css两种常用的不定宽高的水平垂直居中方法,记住它,不再为样式发愁...
  12. 蓝桥杯试题java_java蓝桥杯试题
  13. Android签名总结
  14. Nod32的内网升级方案
  15. 计算机网络基础试题库4答案,计算机网络基础试题库4.doc
  16. linux 安装串口驱动安装失败,z-tek(求救Z-TEK串口安装失败,提示:该设备的驱动程序未被安装(代码28)这个inf中的服务安装段落无效?)...
  17. net use \\192.168.54.145 /user:administrator 12345qwert无法连接,错误码1326
  18. Flutter 中TextField的hintText不居中与光标位置不一致
  19. Windows 一键息屏程序ScreenOff下载及说明
  20. 这些大学奖学金覆盖率100%!

热门文章

  1. c语言编写一个字母金字塔,【强迫症满足向】字母金字塔: C语言实现
  2. solr 4.x 中文分词: IKAnalyzer2012FF_u1.jar
  3. 妙味课堂实战功能开发视频教程 3D翻转焦点图/瀑布流/拖拽购物车/模块化开发等实战教程
  4. Java播放midi文件及加载sf2音色库示例
  5. 抓取微信公众号全部文章,采用AnyProxy+Javascript+Java实现
  6. 基于SSM开发的商品出入库系统
  7. ICP-AES测试基本原理及优缺点
  8. 5W30和5W40原来的区别这么大!
  9. Ubuntu下使用unzip或p7zip解压带密码的zip文件
  10. 制造企业工具如何进行5s管理?