Step 11. ActivityStack.startPausingLocked
这个函数定义在frameworks/base/services/java/com/Android/server/am/ActivityStack.java文件中:
public class ActivityStack { ...... private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) { if (mPausingActivity != null) { ...... } ActivityRecord prev = mResumedActivity; if (prev == null) { ...... } ...... mResumedActivity = null; mPausingActivity = prev; mLastPausedActivity = prev; prev.state = ActivityState.PAUSING; ...... if (prev.app != null && prev.app.thread != null) { ...... try { ...... prev.app.thread.schedulePauseActivity(prev, prev.finishing, userLeaving, prev.configChangeFlags); ...... } catch (Exception e) { ...... } } else { ...... } ...... } ...... }
函数首先把mResumedActivity保存在本地变量prev中。在上一步Step 10中,说到mResumedActivity就是Launcher,因此,这里把Launcher进程中的ApplicationThread对象取出来,通过它来通知Launcher这个Activity它要进入Paused状态了。当然,这里的prev.app.thread是一个ApplicationThread对象的远程接口,通过调用这个远程接口的schedulePauseActivity来通知Launcher进入Paused状态。
参数prev.finishing表示prev所代表的Activity是否正在等待结束的Activity列表中,由于Laucher这个Activity还没结束,所以这里为false;参数prev.configChangeFlags表示哪些config发生了变化,这里我们不关心它的值。
Step 12. ApplicationThreadProxy.schedulePauseActivity
这个函数定义在frameworks/base/core/java/android/app/ApplicationThreadNative.java文件中:
class ApplicationThreadProxy implements IApplicationThread { ...... public final void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving, int configChanges) throws RemoteException { Parcel data = Parcel.obtain(); data.writeInterfaceToken(IApplicationThread.descriptor); data.writeStrongBinder(token); data.writeInt(finished ? 1 : 0); data.writeInt(userLeaving ? 1 :0); data.writeInt(configChanges); mRemote.transact(SCHEDULE_PAUSE_ACTIVITY_TRANSACTION, data, null, IBinder.FLAG_ONEWAY); data.recycle(); } ...... }
这个函数通过Binder进程间通信机制进入到ApplicationThread.schedulePauseActivity函数中。
Step 13. ApplicationThread.schedulePauseActivity
这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中,它是ActivityThread的内部类:
public final class ActivityThread { ...... private final class ApplicationThread extends ApplicationThreadNative { ...... public final void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving, int configChanges) { queueOrSendMessage( finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY, token, (userLeaving ? 1 : 0), configChanges); } ...... } ...... }
这里调用的函数queueOrSendMessage是ActivityThread类的成员函数。上面说到,这里的finished值为false,因此,queueOrSendMessage的第一个参数值为H.PAUSE_ACTIVITY,表示要暂停token所代表的Activity,即Launcher。