android 中主要对activity管理的几个相关类。
ActivityRecord:通常的的 我们用的activity都是以继承activity的形式出现,但是在系统中,每个 activity都是以activity record的形式出现,这个类包涵了 activity的各种信息。
ActivityThread:负责管理并执行主线成中的各种请求。
ActivityManagerService:通过 ActivityManagerNative 类中的代理类 来实现 具体业务的管理者。
ActivityStack: activity的管理及各种状态,一般的activity栈就是说的这个类,同时 还负责一些具体的类的操作 ActivityManagerService 通过调用 ActivityStack 来实习具体的操作,比如activity的开启,关闭,等。
Trace:这个类 主要用来做数据分析,在代码中 ActivityThread 在执行一些人物请求的时候,会调用 Trace中的一些方法来写入和分析数据。
Instrumentation:application instrumentation 的 base. 这个类在应用实例化前开启。他主要用来监听所有系统用应用间的交互。 Instrumentation 通过 instrumentation 标签来进行描述。
通过 onBackPressed 来看一下执行
public void onBackPressed() {
//2.0 < version < 3.0 没有这个判断 默认直接finis
if (!mFragments.popBackStackImmediate()) {
finish() ;
}
}
//2.0 < version < 3.0 没有这个判断 默认直接finis
if (!mFragments.popBackStackImmediate()) {
finish() ;
}
}
public void finish() {
if (mParent == null) {
int resultCode ;
Intent resultData ;
synchronized ( this) {
resultCode = mResultCode ;
resultData = mResultData ;
}
if ( false) Log.v(TAG , "Finishing self: token=" + mToken) ;
try {
if (resultData != null) {
resultData.setAllowFds( false) ;
}
//同样最终调用的是 ActivityManagerService 中的 finishActivity 方法
//和startActivity一样 还是先由 ActivityManagerNativen内部实现的一个代理类
//来实现 远程服务 ActivityManagerService来实现
if (ActivityManagerNative.getDefault()
.finishActivity(mToken , resultCode , resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild( this) ;
}
}
if (mParent == null) {
int resultCode ;
Intent resultData ;
synchronized ( this) {
resultCode = mResultCode ;
resultData = mResultData ;
}
if ( false) Log.v(TAG , "Finishing self: token=" + mToken) ;
try {
if (resultData != null) {
resultData.setAllowFds( false) ;
}
//同样最终调用的是 ActivityManagerService 中的 finishActivity 方法
//和startActivity一样 还是先由 ActivityManagerNativen内部实现的一个代理类
//来实现 远程服务 ActivityManagerService来实现
if (ActivityManagerNative.getDefault()
.finishActivity(mToken , resultCode , resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild( this) ;
}
}
public final boolean finishActivity(IBinder token
, int resultCode
, Intent resultData) {
// Refuse possible leaked file descriptors
if (resultData != null && resultData.hasFileDescriptors() == true) {
throw new IllegalArgumentException( "File descriptors passed in Intent") ;
}
synchronized( this) {
if (mController != null) {
// Find the first activity that is not finishing.
//找到栈顶下一个 可以恢复的Activity
ActivityRecord next = mMainStack.topRunningActivityLocked(token , 0) ;
if (next != null) {
// ask watcher if this is allowed
boolean resumeOK = true;
try {
//判断这个是否可以能恢复
resumeOK = mController.activityResuming(next.packageName) ;
} catch (RemoteException e) {
mController = null;
}
if (!resumeOK) {
return false;
}
}
}
final long origId = Binder.clearCallingIdentity() ;
//最终的执行者 是 ActivityStack 这个才是具体操作的执行者
//ActivityManagerService 将具体的任务 分给 ActivityStack去执行
boolean res = mMainStack.requestFinishActivityLocked(token , resultCode ,
resultData , "app-request") ;
Binder.restoreCallingIdentity(origId) ;
return res ;
}
}
// Refuse possible leaked file descriptors
if (resultData != null && resultData.hasFileDescriptors() == true) {
throw new IllegalArgumentException( "File descriptors passed in Intent") ;
}
synchronized( this) {
if (mController != null) {
// Find the first activity that is not finishing.
//找到栈顶下一个 可以恢复的Activity
ActivityRecord next = mMainStack.topRunningActivityLocked(token , 0) ;
if (next != null) {
// ask watcher if this is allowed
boolean resumeOK = true;
try {
//判断这个是否可以能恢复
resumeOK = mController.activityResuming(next.packageName) ;
} catch (RemoteException e) {
mController = null;
}
if (!resumeOK) {
return false;
}
}
}
final long origId = Binder.clearCallingIdentity() ;
//最终的执行者 是 ActivityStack 这个才是具体操作的执行者
//ActivityManagerService 将具体的任务 分给 ActivityStack去执行
boolean res = mMainStack.requestFinishActivityLocked(token , resultCode ,
resultData , "app-request") ;
Binder.restoreCallingIdentity(origId) ;
return res ;
}
}
final boolean requestFinishActivityLocked(IBinder token
, int resultCode
,
Intent resultData , String reason) {
int index = indexOfTokenLocked(token) ; //判断activity是否存在 存在就去那索引
if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
TAG , "Finishing activity @" + index + ": token=" + token
+ ", result=" + resultCode + ", data=" + resultData
+ ", reason=" + reason) ;
if (index < 0) {
return false;
}
//拿到相对应的ActivityRecord
ActivityRecord r = mHistory.get(index) ;
finishActivityLocked(r , index , resultCode , resultData , reason) ;
return true;
}
Intent resultData , String reason) {
int index = indexOfTokenLocked(token) ; //判断activity是否存在 存在就去那索引
if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(
TAG , "Finishing activity @" + index + ": token=" + token
+ ", result=" + resultCode + ", data=" + resultData
+ ", reason=" + reason) ;
if (index < 0) {
return false;
}
//拿到相对应的ActivityRecord
ActivityRecord r = mHistory.get(index) ;
finishActivityLocked(r , index , resultCode , resultData , reason) ;
return true;
}
final boolean finishActivityLocked(ActivityRecord r
, int index
,
int resultCode , Intent resultData , String reason , boolean immediate) {
if (r.finishing) {
Slog.w(TAG , "Duplicate finish request for " + r) ;
return false;
}
r.makeFinishing() ;
EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY ,
System.identityHashCode(r) ,
r.task.taskId , r.shortComponentName , reason) ;
if (index < (mHistory.size()- 1)) {
ActivityRecord next = mHistory.get(index+ 1) ;
if (next.task == r.task) {
if (r.frontOfTask) {
// The next activity is now the front of the task.
next.frontOfTask = true;
}
if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
// If the caller asked that this activity (and all above it)
// be cleared when the task is reset, don't lose that information,
// but propagate it up to the next activity.
next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) ;
}
}
}
//暂停这个类的一些操作,应该是这样的 这个方法没有细看
r.pauseKeyDispatchingLocked() ;
if (mMainStack) {
if (mService.mFocusedActivity == r) {
mService.setFocusedActivityLocked(topRunningActivityLocked( null)) ;
}
}
finishActivityResultsLocked(r , resultCode , resultData) ;
if (mService.mPendingThumbnails.size() > 0) {
// There are clients waiting to receive thumbnails so, in case
// this is an activity that someone is waiting for, add it
// to the pending list so we can correctly update the clients.
mService.mCancelledThumbnails.add(r) ;
}
//这里传入的是false 先不看这个判断里面的方法
if (immediate) { //这里关闭activity
return finishCurrentActivityLocked(r , index ,
FINISH_IMMEDIATELY) == null;
} else if (mResumedActivity == r) {
boolean endTask = index <= 0
|| (mHistory.get(index- 1)).task != r.task ;
if (DEBUG_TRANSITION) Slog.v(TAG ,
"Prepare close transition: finishing " + r) ; //这里应该是执行关闭动画
mService.mWindowManager.prepareAppTransition(endTask
? WindowManagerPolicy.TRANSIT_TASK_CLOSE
: WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE , false) ;
// Tell window manager to prepare for this one to be removed.
mService.mWindowManager.setAppVisibility(r.appToken , false) ;
if (mPausingActivity == null) {
if (DEBUG_PAUSE) Slog.v(TAG , "Finish needs to pause: " + r) ;
if (DEBUG_USER_LEAVING) Slog.v(TAG , "finish() => pause with userLeaving=false") ;
startPausingLocked( false, false) ;
}
} else if (r.state != ActivityState.PAUSING) {
// If the activity is PAUSING, we will complete the finish once
// it is done pausing; else we can just directly finish it here.
if (DEBUG_PAUSE) Slog.v(TAG , "Finish not pausing: " + r) ;
//这里 和 是不是和 true 执行的方法一样
return finishCurrentActivityLocked(r , index ,
FINISH_AFTER_PAUSE) == null;
} else {
if (DEBUG_PAUSE) Slog.v(TAG , "Finish waiting for pause of: " + r) ;
}
return false;
}
int resultCode , Intent resultData , String reason , boolean immediate) {
if (r.finishing) {
Slog.w(TAG , "Duplicate finish request for " + r) ;
return false;
}
r.makeFinishing() ;
EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY ,
System.identityHashCode(r) ,
r.task.taskId , r.shortComponentName , reason) ;
if (index < (mHistory.size()- 1)) {
ActivityRecord next = mHistory.get(index+ 1) ;
if (next.task == r.task) {
if (r.frontOfTask) {
// The next activity is now the front of the task.
next.frontOfTask = true;
}
if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
// If the caller asked that this activity (and all above it)
// be cleared when the task is reset, don't lose that information,
// but propagate it up to the next activity.
next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) ;
}
}
}
//暂停这个类的一些操作,应该是这样的 这个方法没有细看
r.pauseKeyDispatchingLocked() ;
if (mMainStack) {
if (mService.mFocusedActivity == r) {
mService.setFocusedActivityLocked(topRunningActivityLocked( null)) ;
}
}
finishActivityResultsLocked(r , resultCode , resultData) ;
if (mService.mPendingThumbnails.size() > 0) {
// There are clients waiting to receive thumbnails so, in case
// this is an activity that someone is waiting for, add it
// to the pending list so we can correctly update the clients.
mService.mCancelledThumbnails.add(r) ;
}
//这里传入的是false 先不看这个判断里面的方法
if (immediate) { //这里关闭activity
return finishCurrentActivityLocked(r , index ,
FINISH_IMMEDIATELY) == null;
} else if (mResumedActivity == r) {
boolean endTask = index <= 0
|| (mHistory.get(index- 1)).task != r.task ;
if (DEBUG_TRANSITION) Slog.v(TAG ,
"Prepare close transition: finishing " + r) ; //这里应该是执行关闭动画
mService.mWindowManager.prepareAppTransition(endTask
? WindowManagerPolicy.TRANSIT_TASK_CLOSE
: WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE , false) ;
// Tell window manager to prepare for this one to be removed.
mService.mWindowManager.setAppVisibility(r.appToken , false) ;
if (mPausingActivity == null) {
if (DEBUG_PAUSE) Slog.v(TAG , "Finish needs to pause: " + r) ;
if (DEBUG_USER_LEAVING) Slog.v(TAG , "finish() => pause with userLeaving=false") ;
startPausingLocked( false, false) ;
}
} else if (r.state != ActivityState.PAUSING) {
// If the activity is PAUSING, we will complete the finish once
// it is done pausing; else we can just directly finish it here.
if (DEBUG_PAUSE) Slog.v(TAG , "Finish not pausing: " + r) ;
//这里 和 是不是和 true 执行的方法一样
return finishCurrentActivityLocked(r , index ,
FINISH_AFTER_PAUSE) == null;
} else {
if (DEBUG_PAUSE) Slog.v(TAG , "Finish waiting for pause of: " + r) ;
}
return false;
}
private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r
,
int index , int mode) {
// First things first: if this activity is currently visible,
// and the resumed activity is not yet visible, then hold off on
// finishing until the resumed one becomes visible.
//如果这个将要显示的activity没有显示,就等待他显示之后执行关闭操作
if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
if (!mStoppingActivities.contains(r)) {
mStoppingActivities.add(r) ;
if (mStoppingActivities.size() > 3) {
// If we already have a few activities waiting to stop,
// then give up on things going idle and start clearing
// them out.
scheduleIdleLocked() ;
} else {
checkReadyForSleepLocked() ;
}
}
if (DEBUG_STATES) Slog.v(TAG , "Moving to STOPPING: " + r
+ " (finish requested)") ;
r.state = ActivityState.STOPPING ;
mService.updateOomAdjLocked() ;
return r ;
}
// make sure the record is cleaned out of other places.
//将存储的要finish的activiy的信息移除掉
mStoppingActivities.remove(r) ;
mGoingToSleepActivities.remove(r) ;
mWaitingVisibleActivities.remove(r) ;
if (mResumedActivity == r) {
mResumedActivity = null;
}
final ActivityState prevState = r.state ;
if (DEBUG_STATES) Slog.v(TAG , "Moving to FINISHING: " + r) ;
r.state = ActivityState.FINISHING ;
if (mode == FINISH_IMMEDIATELY
|| prevState == ActivityState.STOPPED
|| prevState == ActivityState.INITIALIZING) {
// If this activity is already stopped, we can just finish
// it right now.
boolean activityRemoved = destroyActivityLocked(r , true, true, "finish-imm") ;
if (activityRemoved) {
resumeTopActivityLocked( null) ;
}
return activityRemoved ? null : r ;
} else {
// Need to go through the full pause cycle to get this
// activity into the stopped state and then finish it.
if (localLOGV) Slog.v(TAG , "Enqueueing pending finish: " + r) ;
mFinishingActivities.add(r) ;
//将resume的activity放到顶部
resumeTopActivityLocked( null) ;
}
return r ;
}
int index , int mode) {
// First things first: if this activity is currently visible,
// and the resumed activity is not yet visible, then hold off on
// finishing until the resumed one becomes visible.
//如果这个将要显示的activity没有显示,就等待他显示之后执行关闭操作
if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
if (!mStoppingActivities.contains(r)) {
mStoppingActivities.add(r) ;
if (mStoppingActivities.size() > 3) {
// If we already have a few activities waiting to stop,
// then give up on things going idle and start clearing
// them out.
scheduleIdleLocked() ;
} else {
checkReadyForSleepLocked() ;
}
}
if (DEBUG_STATES) Slog.v(TAG , "Moving to STOPPING: " + r
+ " (finish requested)") ;
r.state = ActivityState.STOPPING ;
mService.updateOomAdjLocked() ;
return r ;
}
// make sure the record is cleaned out of other places.
//将存储的要finish的activiy的信息移除掉
mStoppingActivities.remove(r) ;
mGoingToSleepActivities.remove(r) ;
mWaitingVisibleActivities.remove(r) ;
if (mResumedActivity == r) {
mResumedActivity = null;
}
final ActivityState prevState = r.state ;
if (DEBUG_STATES) Slog.v(TAG , "Moving to FINISHING: " + r) ;
r.state = ActivityState.FINISHING ;
if (mode == FINISH_IMMEDIATELY
|| prevState == ActivityState.STOPPED
|| prevState == ActivityState.INITIALIZING) {
// If this activity is already stopped, we can just finish
// it right now.
boolean activityRemoved = destroyActivityLocked(r , true, true, "finish-imm") ;
if (activityRemoved) {
resumeTopActivityLocked( null) ;
}
return activityRemoved ? null : r ;
} else {
// Need to go through the full pause cycle to get this
// activity into the stopped state and then finish it.
if (localLOGV) Slog.v(TAG , "Enqueueing pending finish: " + r) ;
mFinishingActivities.add(r) ;
//将resume的activity放到顶部
resumeTopActivityLocked( null) ;
}
return r ;
}
在看 ActivityThread这个类
//这个变量是不是看起来非常眼熟 哈哈
final ApplicationThread mAppThread = new ApplicationThread() ;
final Looper mLooper = Looper.myLooper() ;
final H mH = new H() ;
final HashMap<IBinder , ActivityClientRecord> mActivities
= new HashMap<IBinder , ActivityClientRecord>() ;
// List of new activities (via ActivityRecord.nextIdle) that should
// be reported when next we idle.
ActivityClientRecord mNewActivities = null;
// Number of activities that are currently visible on-screen.
int mNumVisibleActivities = 0 ;
final HashMap<IBinder , Service> mServices
= new HashMap<IBinder , Service>() ;
AppBindData mBoundApplication ;
Profiler mProfiler ;
Configuration mConfiguration ;
Configuration mCompatConfiguration ;
Configuration mResConfiguration ;
CompatibilityInfo mResCompatibilityInfo ;
Application mInitialApplication ;
final ArrayList<Application> mAllApplications
= new ArrayList<Application>() ;
// set of instantiated backup agents, keyed by package name
final HashMap<String , BackupAgent> mBackupAgents = new HashMap<String , BackupAgent>() ;
static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>() ;
Instrumentation mInstrumentation ;
String mInstrumentationAppDir = null;
String mInstrumentationAppLibraryDir = null;
String mInstrumentationAppPackage = null;
String mInstrumentedAppDir = null;
String mInstrumentedAppLibraryDir = null;
boolean mSystemThread = false;
boolean mJitEnabled = false;
final ApplicationThread mAppThread = new ApplicationThread() ;
final Looper mLooper = Looper.myLooper() ;
final H mH = new H() ;
final HashMap<IBinder , ActivityClientRecord> mActivities
= new HashMap<IBinder , ActivityClientRecord>() ;
// List of new activities (via ActivityRecord.nextIdle) that should
// be reported when next we idle.
ActivityClientRecord mNewActivities = null;
// Number of activities that are currently visible on-screen.
int mNumVisibleActivities = 0 ;
final HashMap<IBinder , Service> mServices
= new HashMap<IBinder , Service>() ;
AppBindData mBoundApplication ;
Profiler mProfiler ;
Configuration mConfiguration ;
Configuration mCompatConfiguration ;
Configuration mResConfiguration ;
CompatibilityInfo mResCompatibilityInfo ;
Application mInitialApplication ;
final ArrayList<Application> mAllApplications
= new ArrayList<Application>() ;
// set of instantiated backup agents, keyed by package name
final HashMap<String , BackupAgent> mBackupAgents = new HashMap<String , BackupAgent>() ;
static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>() ;
Instrumentation mInstrumentation ;
String mInstrumentationAppDir = null;
String mInstrumentationAppLibraryDir = null;
String mInstrumentationAppPackage = null;
String mInstrumentedAppDir = null;
String mInstrumentedAppLibraryDir = null;
boolean mSystemThread = false;
boolean mJitEnabled = false;
//这个内部类的方法是不是看起来更加的熟悉
private class ApplicationThread extends ApplicationThreadNative {
private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s" ;
private static final String ONE_COUNT_COLUMN = "%21s %8d" ;
private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d" ;
private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s" ;
// Formatting for checkin service - update version if row format changes
private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1 ;
private void updatePendingConfiguration(Configuration config) {
synchronized (mPackages) {
if (mPendingConfiguration == null ||
mPendingConfiguration.isOtherSeqNewer(config)) {
mPendingConfiguration = config ;
}
}
}
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) ;
}
public final void scheduleStopActivity(IBinder token , boolean showWindow ,
int configChanges) {
queueOrSendMessage(
showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE ,
token , 0 , configChanges) ;
}
public final void scheduleWindowVisibility(IBinder token , boolean showWindow) {
queueOrSendMessage(
showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW ,
token) ;
}
public final void scheduleSleeping(IBinder token , boolean sleeping) {
queueOrSendMessage(H.SLEEPING , token , sleeping ? 1 : 0) ;
}
public final void scheduleResumeActivity(IBinder token , boolean isForward) {
queueOrSendMessage(H.RESUME_ACTIVITY , token , isForward ? 1 : 0) ;
}
public final void scheduleSendResult(IBinder token , List<ResultInfo> results) {
ResultData res = new ResultData() ;
res.token = token ;
res.results = results ;
queueOrSendMessage(H.SEND_RESULT , res) ;
}
private class ApplicationThread extends ApplicationThreadNative {
private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s" ;
private static final String ONE_COUNT_COLUMN = "%21s %8d" ;
private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d" ;
private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s" ;
// Formatting for checkin service - update version if row format changes
private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1 ;
private void updatePendingConfiguration(Configuration config) {
synchronized (mPackages) {
if (mPendingConfiguration == null ||
mPendingConfiguration.isOtherSeqNewer(config)) {
mPendingConfiguration = config ;
}
}
}
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) ;
}
public final void scheduleStopActivity(IBinder token , boolean showWindow ,
int configChanges) {
queueOrSendMessage(
showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE ,
token , 0 , configChanges) ;
}
public final void scheduleWindowVisibility(IBinder token , boolean showWindow) {
queueOrSendMessage(
showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW ,
token) ;
}
public final void scheduleSleeping(IBinder token , boolean sleeping) {
queueOrSendMessage(H.SLEEPING , token , sleeping ? 1 : 0) ;
}
public final void scheduleResumeActivity(IBinder token , boolean isForward) {
queueOrSendMessage(H.RESUME_ACTIVITY , token , isForward ? 1 : 0) ;
}
public final void scheduleSendResult(IBinder token , List<ResultInfo> results) {
ResultData res = new ResultData() ;
res.token = token ;
res.results = results ;
queueOrSendMessage(H.SEND_RESULT , res) ;
}
private void queueOrSendMessage(
int what
, Object obj
, int arg1
, int arg2) {
synchronized ( this) {
if (DEBUG_MESSAGES) Slog.v(
TAG , "SCHEDULE " + what + " " + mH.codeToString(what)
+ ": " + arg1 + " / " + obj) ;
Message msg = Message.obtain() ;
msg.what = what ;
msg.obj = obj ;
msg.arg1 = arg1 ;
msg.arg2 = arg2 ;
mH.sendMessage(msg) ;
}
}
synchronized ( this) {
if (DEBUG_MESSAGES) Slog.v(
TAG , "SCHEDULE " + what + " " + mH.codeToString(what)
+ ": " + arg1 + " / " + obj) ;
Message msg = Message.obtain() ;
msg.what = what ;
msg.obj = obj ;
msg.arg1 = arg1 ;
msg.arg2 = arg2 ;
mH.sendMessage(msg) ;
}
}
private class H extends Handler {
public static final int LAUNCH_ACTIVITY = 100 ;
public static final int PAUSE_ACTIVITY = 101 ;
public static final int PAUSE_ACTIVITY_FINISHING= 102 ;
public static final int STOP_ACTIVITY_SHOW = 103 ;
public static final int STOP_ACTIVITY_HIDE = 104 ;
public static final int SHOW_WINDOW = 105 ;
public static final int HIDE_WINDOW = 106 ;
public static final int RESUME_ACTIVITY = 107 ;
public static final int SEND_RESULT = 108 ;
public static final int DESTROY_ACTIVITY = 109 ;
public static final int BIND_APPLICATION = 110 ;
public static final int EXIT_APPLICATION = 111 ;
public static final int NEW_INTENT = 112 ;
public static final int RECEIVER = 113 ;
public static final int CREATE_SERVICE = 114 ;
public static final int SERVICE_ARGS = 115 ;
public static final int STOP_SERVICE = 116 ;
public static final int REQUEST_THUMBNAIL = 117 ;
public static final int CONFIGURATION_CHANGED = 118 ;
public static final int CLEAN_UP_CONTEXT = 119 ;
public static final int GC_WHEN_IDLE = 120 ;
public static final int BIND_SERVICE = 121 ;
public static final int UNBIND_SERVICE = 122 ;
public static final int DUMP_SERVICE = 123 ;
public static final int LOW_MEMORY = 124 ;
public static final int ACTIVITY_CONFIGURATION_CHANGED = 125 ;
public static final int RELAUNCH_ACTIVITY = 126 ;
public static final int PROFILER_CONTROL = 127 ;
public static final int CREATE_BACKUP_AGENT = 128 ;
public static final int DESTROY_BACKUP_AGENT = 129 ;
public static final int SUICIDE = 130 ;
public static final int REMOVE_PROVIDER = 131 ;
public static final int ENABLE_JIT = 132 ;
public static final int DISPATCH_PACKAGE_BROADCAST = 133 ;
public static final int SCHEDULE_CRASH = 134 ;
public static final int DUMP_HEAP = 135 ;
public static final int DUMP_ACTIVITY = 136 ;
public static final int SLEEPING = 137 ;
public static final int SET_CORE_SETTINGS = 138 ;
public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139 ;
public static final int TRIM_MEMORY = 140 ;
public static final int DUMP_PROVIDER = 141 ;
public static final int UNSTABLE_PROVIDER_DIED = 142 ;
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG , ">>> handling: " + codeToString(msg.what)) ;
switch (msg.what) {
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityStart") ;
ActivityClientRecord r = (ActivityClientRecord)msg.obj ;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo , r.compatInfo) ;
handleLaunchActivity(r , null) ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
} break;
case RELAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityRestart") ;
ActivityClientRecord r = (ActivityClientRecord)msg.obj ;
handleRelaunchActivity(r) ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
} break;
case PAUSE_ACTIVITY:
// Trace 是不是出现了
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityPause") ;
handlePauseActivity((IBinder)msg.obj , false, msg.arg1 != 0 , msg.arg2) ;
maybeSnapshot() ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
break;
if (DEBUG_MESSAGES) Slog.v(TAG , ">>> handling: " + codeToString(msg.what)) ;
switch (msg.what) {
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityStart") ;
ActivityClientRecord r = (ActivityClientRecord)msg.obj ;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo , r.compatInfo) ;
handleLaunchActivity(r , null) ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
} break;
case RELAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityRestart") ;
ActivityClientRecord r = (ActivityClientRecord)msg.obj ;
handleRelaunchActivity(r) ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
} break;
case PAUSE_ACTIVITY:
// Trace 是不是出现了
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER , "activityPause") ;
handlePauseActivity((IBinder)msg.obj , false, msg.arg1 != 0 , msg.arg2) ;
maybeSnapshot() ;
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER) ;
break;
private void handlePauseActivity(IBinder token
, boolean finished
,
boolean userLeaving , int configChanges) {
ActivityClientRecord r = mActivities.get(token) ;
if (r != null) {
//Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
if (userLeaving) {
performUserLeavingActivity(r) ;
}
r.activity.mConfigChangeFlags |= configChanges ;
performPauseActivity(token , finished , r.isPreHoneycomb()) ;
// Make sure any pending writes are now committed.
if (r.isPreHoneycomb()) {
QueuedWork.waitToFinish() ;
}
// Tell the activity manager we have paused.
try {
//最终结果 又走向了 ActivityManagerService
ActivityManagerNative.getDefault().activityPaused(token) ;
} catch (RemoteException ex) {
}
}
}
boolean userLeaving , int configChanges) {
ActivityClientRecord r = mActivities.get(token) ;
if (r != null) {
//Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
if (userLeaving) {
performUserLeavingActivity(r) ;
}
r.activity.mConfigChangeFlags |= configChanges ;
performPauseActivity(token , finished , r.isPreHoneycomb()) ;
// Make sure any pending writes are now committed.
if (r.isPreHoneycomb()) {
QueuedWork.waitToFinish() ;
}
// Tell the activity manager we have paused.
try {
//最终结果 又走向了 ActivityManagerService
ActivityManagerNative.getDefault().activityPaused(token) ;
} catch (RemoteException ex) {
}
}
}
只是简单粗略的来一个分析, 有时间 这几个类 还是要详细的看一下 其中还包含了好几个设计模式。