本篇博文在我之前的博文中已经提到了,但是监听Activity切换又可以作为一个单独的内容来叙述,因此这里又单独拿了出来进行赘述。
Activity的切换无非有两种,第一种:启动或者创建一个新的Activity;第二种:唤醒后台运行的Activity。因此如果我们能够成功监听到启动或者创建一个Activity,或者唤醒Activity我们就基本完成了Activity的切换的监听。
在源码/frameworks/base/core/java/android/app目录下ActivityThread.java中为我们提供了这样一个方法来帮助我们完成对Activity启动、创建、销毁、暂停、停止,唤醒等生命周期的监听。
-
public void handleMessage(Message msg) {
-
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
-
switch (msg.what) {
-
case LAUNCH_ACTIVITY: {
-
-
if ( true == mEnableAppLaunchLog && !mIsUserBuild && false == mTraceEnabled ) {
-
try {
-
FileInputStream fprofsts_in = new FileInputStream("/proc/mtprof/status");
-
if ( fprofsts_in.read()== '3' ) {
-
Log.v(TAG, "start Profiling for empty process");
-
mTraceEnabled = true;
-
Debug.startMethodTracing("/data/data/applaunch");
-
}
-
} catch (FileNotFoundException e) {
-
Slog.e(TAG, "mtprof entry can not be found", e);
-
} catch (java.io.IOException e) {
-
Slog.e(TAG, "mtprof entry open failed", e);
-
}
-
}
-
-
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER | Trace.TRACE_TAG_PERF, "activityStart");
-
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
-
-
r.packageInfo = getPackageInfoNoCheck(
-
r.activityInfo.applicationInfo, r.compatInfo);
-
handleLaunchActivity(r, null);
-
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER | Trace.TRACE_TAG_PERF);
-
} 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.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;
-
case PAUSE_ACTIVITY_FINISHING:
------分隔线----------------------------
------分隔线----------------------------