您的当前位置:首页正文

Android开发之ActivityManager

2024-12-01 来源:个人技术集锦

ActivityManager服务是对Activity管理、运行时功能管理和运行时数据结构的封装,进程(Process)、应用程序/包、服务(Service)、任务(Task)信息等。包括以下功能:

 

  • 激活/去激活activity
  • 注册/取消注册动态接受intent
  • 发送/取消发送intent
  • activity生命周期管理(暂停,恢复,停止,销毁等)
  • activity task管理(前台->后台,后台->前台,最近task查询,运行时task查询)
  • 激活/去激活service
  • 激活/去激活provider等

常用内部静态类有:

ActivityManager.MemoryInfo: 系统可用内存信息

ActivityManager.RecentTaskInfo: 最近的任务信息

ActivityManager.RunningAppProcessInfo: 正在运行的进程信息

ActivityManager.RunningServiceInfo: 正在运行的服务信息

ActivityManager.RunningTaskInfo: 正在运行的任务信息

 

用法:

1.获取ActivityManager服务:

2.获取内存信息:

MemoryInfo memoryInfo = new MemoryInfo() ;
     mActivityManager.getMemoryInfo(memoryInfo);

3.获取正在运行的task信息:

 List<RunningTaskInfo> runningTaskInfos = mActivityManager.getRunningTasks(Integer.MAX_VALUE);
    String cmpNameTemp = null;
    if (null != runningTaskInfos)
    {
        cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString();
    }
4.判断android应用是否在前台:
public static boolean isAppOnForeground(Context context)
    {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = context.getPackageName();
        List<RecentTaskInfo> appTask = activityManager.getRecentTasks(Integer.MAX_VALUE, 1);
        
        if (appTask == null)
        {
            return false;
        }
        
        if (appTask.get(0).baseIntent.toString().contains(packageName))
        {
            return true;
        }
        return false;
    }
5.获取android手机内安装的所有桌面:
private static List<String> getAllTheLauncher(Context context)
    {
        List<String> names = null;
        PackageManager pkgMgt = context.getPackageManager();
        Intent it = new Intent(Intent.ACTION_MAIN);
        it.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
        if (ra.size() != 0)
        {
            names = new ArrayList<String>();
        }
        for (int i = 0; i < ra.size(); i++)
        {
            String packageName = ra.get(i).activityInfo.packageName;
            names.add(packageName);
        }
        return names;
    }

6.判断程序前后台状态:

public static boolean isLauncherRunning(Context context)
    {
        boolean result = false;
        List<String> names = getAllTheLauncher(context);
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses();
        for (RunningAppProcessInfo running : appList)
        {
            if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
            {
                for (int i = 0; i < names.size(); i++)
                {
                    if (names.get(i).equals(running.processName))
                    {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }
7.获取系统中进程信息:

private void getRunningAppProcessInfo() {  
        processInfoList = new ArrayList<ProcessInfo>(); 

   List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager.getRunningAppProcesses(); 

   for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) { 

        int pid = appProcessInfo.pid; 

        int uid = appProcessInfo.uid; 

       String processName = appProcessInfo.processName; 

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

       Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(myMempid); 

       int memSize = memoryInfo[0].dalvikPrivateDirty; 

       Log.i(TAG, "processName: " + processName + "  pid: " + pid 

                    + " uid:" + uid + " memorySize is -->" + memSize + "kb"); 

            // 构造一个ProcessInfo对象

            ProcessInfo processInfo = new ProcessInfo(); 

            processInfo.setPid(pid); 

            processInfo.setUid(uid); 

            processInfo.setMemSize(memSize); 

            processInfo.setPocessName(processName); 

            processInfoList.add(processInfo); 

 

           // 获得每个进程里运行的应用程序(包),即每个应用程序的包名

            String[] packageList = appProcessInfo.pkgList; 

            Log.i(TAG, "process id is " + pid + "has " + packageList.length); 

            for (String pkg : packageList) { 

                Log.i(TAG, "packageName " + pkg + " in process id is -->"+ pid); 

            } 

        } 

    } 

转载于:https://www.cnblogs.com/waleyx/p/3649520.html

显示全文