Android一些关键函数和类的分析-ActivityManager

ActivityManager能实现的功能可以从其具备的函数中进行分析:


1.getFrontActivityScreenCompatMode()

-->获取最上层Activity的兼容模式,在ActivityManagerService中调用CompatModePackages的getFrontActivityScreenCompatModeLocked

    public int getFrontActivityScreenCompatModeLocked() {
        ActivityRecord r = mService.mMainStack.topRunningActivityLocked(null);
        if (r == null) {
            return ActivityManager.COMPAT_MODE_UNKNOWN;
        }
        return computeCompatModeLocked(r.info.applicationInfo);
    }
获取topRunningActivity后取其兼容模式而已


2.setFrontActivityScreenCompatMode(int mode)

-->设置兼容模式


3.getPackageScreenCompatMode(String packageName)

-->在ActivityManagerService中调用CompatModePackages的getPackageScreenCompatModeLocked

public int getPackageScreenCompatModeLocked(String packageName) {
        ApplicationInfo ai = null;
        try {
            ai = AppGlobals.getPackageManager().getApplicationInfo(packageName, 0, 0);
        } catch (RemoteException e) {
        }
        if (ai == null) {
            return ActivityManager.COMPAT_MODE_UNKNOWN;
        }
        return computeCompatModeLocked(ai);
    }
获取指定包名的视图的兼容模式


4.setPackageScreenCompatMode(String packageName, int mode)

-->设置指定包名视图的兼容模式


5.boolean getPackageAskScreenCompat(String packageName)

其实现为:

    // Compatibility state: no longer ask user to select the mode.
    public static final int COMPAT_FLAG_DONT_ASK = 1<<0;
    public boolean getPackageAskCompatModeLocked(String packageName) {
        return (getPackageFlags(packageName)&COMPAT_FLAG_DONT_ASK) == 0;
    }
所以就是获取是否需要用户来选择兼容模式
6.setPackageAskScreenCompat(String packageName, boolean ask)

这个函数自然就是设置"是否让用户选择兼容模式"这个boolean项了


7.int getMemoryClass(),调用的staticGetMemoryClass()

    /**
     * Return the approximate per-application memory class of the current
     * device.  This gives you an idea of how hard a memory limit you should
     * impose on your application to let the overall system work best.  The
     * returned value is in megabytes; the baseline Android memory class is
     * 16 (which happens to be the Java heap limit of those devices); some
     * device with more memory may return 24 or even higher numbers.
     */
    /** @hide */
    static public int staticGetMemoryClass() {
        // Really brain dead right now -- just take this from the configured
        // vm heap size, and assume it is in megabytes and thus ends with "m".
        String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
        if (vmHeapSize != null && !"".equals(vmHeapSize)) {
            return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
        }
        return staticGetLargeMemoryClass();
    }
其实就是获取一个系统属性dalvik.vm.heapgrowthlimit

8getLargeMemoryClass(),调用的staticGetLargeMemoryClass,同样是获取一个系统属性dalvik.vm.heapsize


9.static public boolean isHighEndGfx()

通过判断RAM大小来判断是否能够用使用硬件渲染

 /**
     * Used by persistent processes to determine if they are running on a
     * higher-end device so should be okay using hardware drawing acceleration
     * (which tends to consume a lot more RAM).
     * @hide
     */

    static public boolean isHighEndGfx() {
        MemInfoReader reader = new MemInfoReader();
        reader.readMemInfo();
        if (reader.getTotalSize() >= (512*1024*1024)) {
            // If the device has at least 512MB RAM available to the kernel,
            // we can afford the overhead of graphics acceleration.
            return true;
        }

        Display display = DisplayManagerGlobal.getInstance().getRealDisplay(
                Display.DEFAULT_DISPLAY);
        Point p = new Point();
        display.getRealSize(p);
        int pixels = p.x * p.y;
        if (pixels >= (1024*600)) {
            // If this is a sufficiently large screen, then there are enough
            // pixels on it that we'd really like to use hw drawing.
            return true;
        }
        return false;
    }
如果RAM大于512MB,那么就可以使用硬件渲染,如果RAM小于512M,那么就判断屏幕分辨率是否大于1024*600,如果是的话,就可以使用硬件渲染,如果小于的话,当然就判定为低端配置了,也就不使用硬件渲染。这个函数可能在后续定制诸如Android穿戴式设备时会需要用到,因为内存小,而且屏幕小,分辨率还不一定高。

10.public boolean isLargeRAM()

    static public boolean isLargeRAM() {
        MemInfoReader reader = new MemInfoReader();
        reader.readMemInfo();
        if (reader.getTotalSize() >= (640*1024*1024)) {
            // Currently 640MB RAM available to the kernel is the point at
            // which we have plenty of RAM to spare.
            return true;
        }
        return false;
    }

11.List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)

     * Return a list of the tasks that the user has recently launched, with
     * the most recent being first and older ones after in order.

12. public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)

     * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
     * specific user. It requires holding
     * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.

13.public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver)

     * Return a list of the tasks that are currently running, with
     * the most recent being first and older ones after in order.  Note that
     * "running" does not mean any of the task's code is currently loaded or
     * activity -- the task may have been frozen by the system, so that it
     * can be restarted in its previous state when next brought to the
     * foreground.

14.public boolean removeTask(int taskId, int flags)

15.public TaskThumbnails getTaskThumbnails(int id)获取一个指定Task的缓存,TaskThumbnails对象是定义在ActivityManager中的

16.public Bitmap getTaskTopThumbnail(int id)

这个函数参见ActivityManagerService.java中的实现为:

    public Bitmap getTaskTopThumbnail(int id) {
        synchronized (this) {
            enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
                    "getTaskTopThumbnail()");
            TaskRecord tr = taskForIdLocked(id);
            if (tr != null) {
                return mMainStack.getTaskTopThumbnailLocked(tr);
            }
        }
        return null;
    }
    public Bitmap getTaskTopThumbnailLocked(TaskRecord tr) {
        ActivityRecord resumed = mResumedActivity;
        if (resumed != null && resumed.task == tr) {
            // This task is the current resumed task, we just need to take
            // a screenshot of it and return that.
            return resumed.stack.screenshotActivities(resumed);
        }
        // Return the information about the task, to figure out the top
        // thumbnail to return.
        TaskAccessInfo info = getTaskAccessInfoLocked(tr.taskId, true);
        if (info.numSubThumbbails <= 0) {
            return info.mainThumbnail != null ? info.mainThumbnail : tr.lastThumbnail;
        } else {
            return info.subtasks.get(info.numSubThumbbails-1).holder.lastThumbnail;
        }
    }
(这个TopThumbnail的top还有待验证和确认到底是什么意思)

 17,public void moveTaskToFront(int taskId, int flags),

     * Ask that the task associated with a given task ID be moved to the
     * front of the stack, so it is now visible to the user.  Requires that
     * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
     * or a SecurityException will be thrown.

将指定任务调度到任务栈栈顶

18.public List<RunningServiceInfo> getRunningServices(int maxNum)

获取当前在运行的Service

19. public PendingIntent getRunningServiceControlPanel(ComponentName service)

    /**
     * Returns a PendingIntent you can start to show a control panel for the
     * given running service.  If the service does not have a control panel,
     * null is returned.
     */

http://blog.csdn.net/yuzhiboyi/article/details/8484771

20public void getMemoryInfo(MemoryInfo outInfo)

获取内存信息

21. public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer)

清除用户信息

22. public List<ProcessErrorStateInfo> getProcessesInErrorState()

犯规当前所有处在error状态的应用进程

23.public List<ApplicationInfo> getRunningExternalApplications() 

获取正在运行的安装在拓展设备上的应用程序,比如SD卡

24. public List<RunningAppProcessInfo> getRunningAppProcesses()

获取当前正在运行的应用进程

25. static public void getMyMemoryState(RunningAppProcessInfo outState) 

这个以RunningAppProcessInfo类型的对象作为参数,说明获取的是当前正在运行的指定应用的内存状态

26.public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)

获取一些列指定ID的进程的内存使用情况,这个可能在安全管理软件统计内存占用情况的时候会用到

27.public void restartPackage(String packageName) {
        killBackgroundProcesses(packageName);
    }

这是对killBackgroundProcesses的一个封装而已

28,public void killBackgroundProcesses(String packageName) 

杀掉一个后台进程

29.public void forceStopPackage(String packageName)

强制停止一个进程

30. public ConfigurationInfo getDeviceConfigurationInfo()

返回设备配置信息

31. public int getLauncherLargeIconDensity()

获取Launcher分辨率

在自定义Launcher的时候会用到,使用方式参见:

http://www.tuicool.com/articles/fuaIfiM

32. public int getLauncherLargeIconSize()

获取Launcher的icon大小


33.public static boolean isUserAMonkey()

     * Returns "true" if the user interface is currently being messed with
     * by a monkey.

http://www.th7.cn/Program/Android/201410/301519.shtml

33.public Map<String, Integer> getAllPackageLaunchCounts()

返回设备上当前应用的被启动次数统计情况

由其实现

 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
            for (PkgUsageStats pkgUsageStats : allPkgUsageStats) {
                launchCounts.put(pkgUsageStats.packageName, pkgUsageStats.launchCount);
            }
34. public static int checkComponentPermission(String permission, int uid,
            int owningUid, boolean exported)

Android的权限检查,关于Android的权限机制参见这篇文章:

http://blog.csdn.net/xiayu98020214/article/details/8487165

35. public static int checkUidPermission(String permission, int uid)

检查指定用户的权限

34,35会用到PackageManager,后续分析

36. public static int handleIncomingUser(int callingPid, int callingUid, int userId,
            boolean allowAll, boolean requireFull, String name, String callerPackage) 

校验用户对指定进程的操作权限

http://www.cnblogs.com/zealotrouge/p/3182617.html


37, public static int getCurrentUser()

获取当前用户

38. public PkgUsageStats[] getAllPackageUsageStats()

获取当前安装应用的使用情况

39.public boolean switchUser(int userid) 

切换用户

40.public boolean isUserRunning(int userid) 

判断指定用户是否在started状态












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值