ActivityManagerService分析,Android开发知识点

本文详细分析了Android系统中ActivityManagerService如何管理任务(Task)和Activity,包括权限检查规则、Intent启动标志的影响、Task复用策略、启动模式的理解,以及标准模式和singleInstance模式的特殊处理。通过对源码的解析,揭示了Android应用启动、Task管理的核心逻辑。
摘要由CSDN通过智能技术生成
  1. }

如果 当前的activity是从一个application启动的 , 也就是说参数caller!=null,此时的callingPid和callingUid可以从caller所处的进程中得出。

[java]  view plain copy

  1. ProcessRecord callerApp = null;

  2. if (caller != null) {

  3. callerApp = mService.getRecordForAppLocked(caller);

  4. if (callerApp != null) {

  5. callingPid = callerApp.pid;

  6. callingUid = callerApp.info.uid;

  7. } else {

  8. Slog.w(TAG, "Unable to find app for caller " + caller

  9. + " (pid=" + callingPid + ") when starting: "

  10. + intent.toString());

  11. err = START_PERMISSION_DENIED;

  12. }

  13. }

1.2.2.2    permission检查的规则
  1. Root uid(0), System Server uid (Process.SYSTEM_UID), own process(MY_PID),将授权permission

  2. 如果请求启动的activity的属性android:exported=false, 并且请求的callingUid不等于请求启动的activity的UID,不允许启动;

3. 请求启动的activity没有设定permission,只有当activity的permission和其所在的application的android:permission均为设置时才为null,直设置了application未设置activity,那么activity的permission与application相同。activity的permission为空,则授权;

4. 请求启动的activity设定了permission,那么检查请求方的activity中是否声明了使用这个permission,如果声明,授权。

[java]  view plain copy

  1. final int perm = mService.checkComponentPermission(aInfo.permission, callingPid,

  2. callingUid, aInfo.exported ? -1 : aInfo.applicationInfo.uid);

  3. if (perm != PackageManager.PERMISSION_GRANTED) {

  4. if (resultRecord != null) {

  5. sendActivityResultLocked(-1,

  6. resultRecord, resultWho, requestCode,

  7. Activity.RESULT_CANCELED, null);

  8. }

  9. String msg = "Permission Denial: starting " + intent.toString()

  10. + " from " + callerApp + " (pid=" + callingPid

  11. + “, uid=” + callingUid + “)”

  12. + " requires " + aInfo.permission;

  13. Slog.w(TAG, msg);

  14. throw new SecurityException(msg);

  15. }

创建ActivityRecord

[java]  view plain copy

  1. ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,

  2. intent, resolvedType, aInfo, mService.mConfiguration,

  3. resultRecord, resultWho, requestCode, componentSpecified);

1.3  task管理


在android应用开发中,task是一个很重要的概念,在文章开始,我就画出了task和activity以及process整体的关系,在这里还需要说明一下,task和application的区别,application在android中的作用仅仅是activity在未被使用前的一个容器,我们开发android应用程序时,需要一个application来组织我们开发的activity,application和activity之间是一个静态关系,并且是一一对应的关系;也就是说我们开发的activity在PM中的最终形式是唯一的,永远对应一个application。

而task和activity之间的关系是动态的关系,是我们在运行应用程序时,activity的调用栈,同一个task中的activity可能来自不同的application。

关于task,更详细的资料参考:http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

这部分源码主要是在

final int startActivityUncheckedLocked(ActivityRecord r,

ActivityRecord sourceRecord, Uri[] grantedUriPermissions,

int grantedMode, boolean onlyIfNeeded, boolean doResume)

这一部分比较繁琐,写的可能比较乱。

1.3.1 Intent.FLAG_ACTIVITY_NO_USER_ACTION

检查Intent是否设置了Intent.FLAG_ACTIVITY_NO_USER_ACTION,如果设置了,则在activity pause之前将不再调用activity的onUserLeaveHint()方法。

[java]  view plain copy

  1. mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;

  2. if (DEBUG_USER_LEAVING) Slog.v(TAG,

  3. “startActivity() => mUserLeaving=” + mUserLeaving);

1.3.2 Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP

检查Intent是否设置了Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP,这个标志我有点困惑,从它的注释可以看出它的含义是指如果设置了该flag,那么mHistory中最top的a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值