Android6.0获取进程和进程数目的方法:
2017-02-21 15:19
441 查看
Android5.0之后使用
ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取进程的集合 runningAppProcesses= activityManager.getRunningAppProcesses(); size = runningAppProcesses.size();
这种方法获取进程数,已经不可能了android开发越来越艰难,前天写进程管理的代码。在这里加以总结
1.若用户未开启权限,需要先开启权限:
protected void oncreat(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (!hasPermission()) { startActivityForResult( new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS), MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS) { if (!hasPermission()) { ##若用户未开启权限,则引导用户开启“Apps with usage access”权限 startActivityForResult( new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS), MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS); } } } ##//检测用户是否对本app开启了“Apps with usage access”权限 private boolean hasPermission() { AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = 0; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), getPackageName()); } return mode == AppOpsManager.MODE_ALLOWED; }2.提供的方法的集合
public static int size;
/**
* 获取进程数的方法
* @param context
* @return
*/
public static int getProcessCount(Context context){
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
size = getTopApp(context);
}else {
ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取进程的集合 runningAppProcesses= activityManager.getRunningAppProcesses(); size = runningAppProcesses.size();
}
return size;
}
/**
* 6.0获取进程数
* @param context
* @return
*/
private static int getTopApp(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager m = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (m != null) {
long now = System.currentTimeMillis();
//获取60秒之内的应用数据
List<UsageStats> stats = m.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 60 * 1000, now);
Log.i(TAG, "Running app number in last 60 seconds : " + stats.size());
String topActivity = "";
//取得最近运行的一个app,即当前运行的app
if ((stats != null) && (!stats.isEmpty())) {
int j = 0;
for (int i = 0; i < stats.size(); i++) {
if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
j = i;
}
topActivity = stats.get(j).getPackageName();
Log.i(TAG, "top running app is : "+topActivity);
}
}
return stats.size();
}
}
return 0;
}
/**
*获取栈顶的包名
*/
public static String getTopActivityApp(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager m = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (m != null) {
long now = System.currentTimeMillis();
//获取60秒之内的应用数据
List<UsageStats> stats = m.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 60 * 1000, now);
Log.i(TAG, "Running app number in last 60 seconds : " + stats.size());
String topActivity = "";
//取得最近运行的一个app,即当前运行的app
if ((stats != null) && (!stats.isEmpty())) {
int j = 0;
for (int i = 0; i < stats.size(); i++) {
if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
j = i;
}
}
topActivity = stats.get(j).getPackageName();
return topActivity;
}
Log.i(TAG, "top running app is : "+topActivity);
}
}
return null;
}
/**
* 获取可用内存的数据大小 ,单位为byte
* @param context
* @return
*/
public static long getAvailSpace(Context context){
//获取activityManager
ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//构建可用内存对象
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long availMem = memoryInfo.availMem;
return availMem;
}
public static long getTotalSpace(Context context){
//获取activityManager
ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//构建可用内存对象
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.totalMem;
}
/**
* 获取全部的内存空间
* @return
*/
public static long getTotalSpace(){
FileReader fileReader=null;
BufferedReader bufferedReader=null;
try {
fileReader=new FileReader("proc/meminfo");
bufferedReader=new BufferedReader(fileReader);
String lineOne = bufferedReader.readLine();
char[] chars = lineOne.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (char aChar : chars) {
if (aChar>='0'&&aChar<='9'){
stringBuffer.append(aChar);
}
}
return Long.parseLong(stringBuffer.toString())*1024;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fileReader!=null&&bufferedReader!=null){
try {
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return 0;
}
/**
* 获取进程的信息
* @param context
* @return
*/
public static List<ProcessInfoBean> getProcessInfo(Context context){
ArrayList<ProcessInfoBean> processInfoList = new ArrayList<>();
ActivityManager systemService = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = systemService.getRunningAppProcesses();
PackageManager PM = context.getPackageManager();
for (ActivityManager.RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
ProcessInfoBean processInfo=new ProcessInfoBean();
processInfo.packageName= runningAppProcess.processName;
//获取系统占用的内存大小
int pid = runningAppProcess.pid;
Debug.MemoryInfo[] processMemoryInfo = systemService.getProcessMemoryInfo(new int[]{pid});
//获取已使用的大小:
processInfo.memeSize= processMemoryInfo[0].getTotalPrivateDirty()*1024;
//获取应用的名称
try {
ApplicationInfo applicationInfo = PM.getApplicationInfo(processInfo.getPackageName(), 0);
processInfo.name= applicationInfo.loadLabel(PM).toString();
processInfo.icon= applicationInfo.loadIcon(PM);
if ((applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==ApplicationInfo.FLAG_SYSTEM){
p
c3b4
rocessInfo.isSystem=true;
}else {
processInfo.isSystem=false;
}
} catch (PackageManager.NameNotFoundException e) {
processInfo.name=processInfo.packageName;
processInfo.icon=context.getResources().getDrawable(R.mipmap.ic_launcher);
processInfo.isSystem=true;
e.printStackTrace();
}
processInfoList.add(processInfo);
}
return processInfoList;
}
/**
* 6.0版本获取相应的进程信息
* @param context
* @return
*/
public static List<ProcessInfoBean> getProcess6Info(Context context){
ArrayList<ProcessInfoBean> processInfoList = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager m = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (m != null) {
long now = System.currentTimeMillis();
//获取60秒之内的应用数据
List<UsageStats> stats = m.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 60 * 1000, now);
ActivityManager systemService = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Log.i(TAG, "Running app number in last 60 seconds : " + stats.size());
//取得最近运行的一个app,即当前运行的app
if ((stats != null) && (!stats.isEmpty())) {
for (int i = 0; i < stats.size(); i++) {
/* if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
j = i;
}*/
int i1 = stats.get(i).describeContents();
String processName = stats.get(i).getPackageName();
Log.i(TAG, "top running app is : " + processName);
PackageManager PM = context.getPackageManager();
ProcessInfoBean processInfo=new ProcessInfoBean();
int uidForName = Process.getUidForName(processName);
/***
* 此方法未能成功获取进程的内存信息
*/
Debug.MemoryInfo[] processMemoryInfo = systemService.getProcessMemoryInfo(new int[]{uidForName});
//获取已使用的大小:
processInfo.memeSize= processMemoryInfo[0].getTotalPrivateDirty()*1024;
processInfo.packageName= processName;
processInfo.appPid=uidForName;
//获取应用的名称
try {
ApplicationInfo applicationInfo = PM.getApplicationInfo(processInfo.getPackageName(), 0);
processInfo.name= applicationInfo.loadLabel(PM).toString();
processInfo.icon= applicationInfo.loadIcon(PM);
if ((applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==ApplicationInfo.FLAG_SYSTEM){
processInfo.isSystem=true;
}else {
processInfo.isSystem=false;
}
} catch (PackageManager.NameNotFoundException e) {
processInfo.name=processInfo.packageName;
processInfo.icon=context.getResources().getDrawable(R.mipmap.ic_launcher);
processInfo.isSystem=true;
e.printStackTrace();
}
processInfoList.add(processInfo);
}
}
}
}
return processInfoList;
}
相关文章推荐
- Android6.0获取进程和进程数目的方法
- Android6.0获取前台进程名称的方法
- Android6.0以上获取前台进程的方法
- 解析Linux内核获取当前进程指针的方法
- 获取其它进程内EDIT BOX内容的一种方法
- 根据Handle获取进程名的方法
- Delphi获取其它进程窗口句柄的3种方法
- C#获取当前进程HASH的方法
- Linux进程资源健康,数据获取方法
- C++获取当前进程的进程号方法
- Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法 .
- Linux SHELL中获取进程号的方法
- Linux进程资源健康,数据获取方法
- Sun的JDK里获取当前进程ID的方法(hack)
- Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法
- Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法
- QNX下获取系统信息的方法(cpu,内存,进程等等)
- 解析Linux内核获取当前进程指针的方法
- 以SYSTEM权限运行的进程获取登录用户名的方法
- Delphi获取其它进程窗口句柄的3种方法