Calvin's Tech Space

          成于堅忍,毀于浮躁

             :: 首頁 :: 聯系 :: 聚合  :: 管理
          啟動activity:
          **android.process.acore進程
          1.Acticity.startActivityForResult()
          2.Instrumention.execStartActivity();
          3.ActivityManagerNative.getDefault()
                          .startActivity()
            3.1 ActivityManagerNative.getDefault()返回的是ActivityManagerProxy的實例,
            它只是一個代理類,這個代理類實際上代理的是IBinder b = ServiceManager.getService("activity");
            這個Service。
            3.2 這個Service是什么時候添加進來的呢?
            在SystemServer.java的run()中有調用
            ActivityManagerService.setSystemProcess();
                ServiceManager.addService("activity", m);
                這里還會添加許多系統關鍵服務。
                (TODO:查看在SystemServer的Log輸出)
               
          **system_process進程
          4 ActivityManagerNative
              |--ActivityManagerService
             
              在ActivityManagerNative的onTransact方法中最終提供了服務:
              case START_ACTIVITY_TRANSACTION:
                ActivityManagerService.startActivity();
                  --startActivityLocked(IApplicationThread caller,
                      Intent intent, String resolvedType,
                      Uri[] grantedUriPermissions,
                      int grantedMode, ActivityInfo aInfo, IBinder resultTo,
                      String resultWho, int requestCode,
                      int callingPid, int callingUid, boolean onlyIfNeeded,
                      boolean componentSpecified)
                    --startActivityUncheckedLocked(r, sourceRecord,
                          grantedUriPermissions, grantedMode, onlyIfNeeded, true)
                    在這個方法里面檢查權限,解析intent中的Flag。。。
                    --startActivityLocked(HistoryRecord r, boolean newTask)
                      --resumeTopActivityLocked(HistoryRecord prev)
                        --startSpecificActivityLocked(HistoryRecord r,boolean andResume, boolean checkConfig)
                          --startProcessLocked(String processName,ApplicationInfo info, boolean knownToBeDead, int intentFlags,String hostingType, ComponentName hostingName)
                            --startProcessLocked(ProcessRecord app,String hostingType, String hostingNameStr)
                              在這里啟動一個進程用來host這個應用
                              int pid = Process.start("android.app.ActivityThread",
                              mSimpleProcessManagement ? app.processName : null, uid, uid,
                              gids, debugFlags, null);
                             
          ActivityManagerService.java
          --startSpecificActivityLocked(HistoryRecord r,boolean andResume, boolean checkConfig)
            --realStartActivityLocked(HistoryRecord r,ProcessRecord app, boolean andResume, boolean checkConfig)
              --app.thread.scheduleLaunchActivity                                   //scheduleLaunchActivity()@IApplicationThread.java
                --scheduleLaunchActivity()@ActivityThread.java                      //這里實際是ApplicationThreadNative提供的服務
                  --handleMessage()@H$ActivityThread.java
                    --handleLaunchActivity()@ActivityThread.java
                      --Activity performLaunchActivity(ActivityRecord r, Intent customIntent)@ActivityThread.java  //這時真正的Activity對象被構造出來
                        --mInstrumentation.newActivity()                            //通過反射構造出Activity對象
                        --activity.attach()                                         //初始化Activity,生成一個window對象,設置各種狀態等等
                        --mInstrumentation.callActivityOnCreate(activity, r.state);  //調用Activity的onCreate()方法
                     
                        到這里,我們自己寫的activity的onCreate()方法已經被系統調用了,接下來依次回調生命周期方法:
                        --activity.performStart();
                          --mInstrumentation.callActivityOnStart(this);
                        --mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
                        --mInstrumentation.callActivityOnPostCreate(activity, r.state);
                        --mActivities.put(r.token, r);                               //將這個activity入棧
                       
                      然后就要調用onResume()方法了:
                      --handleResumeActivity(IBinder token, boolean clearHide, boolean isForward)
                        --performResumeActivity(token, clearHide);
                          --r.activity.performResume();
                            --performRestart()@Activity.java;
                              --mInstrumentation.callActivityOnRestart(this);
                              --mInstrumentation.callActivityOnStart(this);
                          --mInstrumentation.callActivityOnResume(this);
                          onResume()已經調用完畢,一個activity的邏輯處理結束了,但是這時候屏幕上還不會顯示任何東西,因為View還沒有添加進去
                         
                         
                        --r.window.getDecorView();                                   //開始把DecorView添加進Window
                        --wm.addView(decor, l);
                     
                      至此一個Activity啟動結束。
          posted on 2010-11-23 14:59 calvin 閱讀(4222) 評論(1)  編輯  收藏 所屬分類: Android

          Feedback

          # re: [原]Activity啟動過程分析 2011-07-08 21:26 mark in working
          寫的很好,向您學習!  回復  更多評論
            

          主站蜘蛛池模板: 志丹县| 桦南县| 西安市| 久治县| 蛟河市| 介休市| 甘南县| 兴城市| 灵丘县| 武安市| 宜黄县| 广平县| 乐山市| 牙克石市| 望谟县| 柘荣县| 九寨沟县| 湟源县| 肃北| 修武县| 宜黄县| 融水| 临湘市| 浪卡子县| 博白县| 宁武县| 休宁县| 拉萨市| 汤原县| 博兴县| 湖北省| 天等县| 南丹县| 龙陵县| 恩施市| 重庆市| 象州县| 沙坪坝区| 商河县| 综艺| 剑河县|