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);
- }
- }
- }
- ##
- 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;
-
-
-
-
-
-
- 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;
-
- }
-
-
-
-
-
-
- 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();
-
- 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 = "";
-
- 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();
-
- 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 = "";
-
-
- 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;
- }
-
-
-
-
-
- public static long getAvailSpace(Context context){
-
- 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) context.getSystemService(Context.ACTIVITY_SERVICE);
-
- ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
-
- activityManager.getMemoryInfo(memoryInfo);
-
- return memoryInfo.totalMem;
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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){
- 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;
- }
-
-
-
-
-
-
- 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();
-
- 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());
-
- if ((stats != null) && (!stats.isEmpty())) {
- for (int i = 0; i < stats.size(); 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;
- }