关于ActivityManagerService的学习启动模式 7.23

来源:互联网 发布:金润软件成都 编辑:程序博客网 时间:2024/04/30 13:19

从播放器—>Home—>Settings—>setVolme—>exit,退到了播放器,按正常的逻辑来讲,应该退到home


这样的解决办法只能从ActivityManagerSerice入手了:


**android.process.acore进程


1,Acticity.startActivityForResult()


2,Instrumention.execStartActivity();


3,ActivityManagerNative.getDefault().startActivity()


   3.1 ActivityManagerNative.getDefault()返回的是ActivityManagerProxy的实例,


       它只是一个代理类,这个代理类实际上代理的是IBinder b = ServiceManager.getService("activity");


   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)

activity启动模式的逻辑主要在startActivityUncheckedLocked()函数中

====================>具体可以参考这里博客的具体说明

http://blog.csdn.net/guoqifa29/article/details/44004489

          --startActivityUncheckedLocked(r, sourceRecord, grantedUriPermissions, grantedMode, onlyIfNeeded, true) //在这个方法里面检查权限,解析intent中的Flag.  


=============》文章的核心在这里:可以自己定义一个flag,继承Intent类即可:比如.FLAG_ACTIVITY_INHERIT_MICHAEL


然后发送的时候就:intent.addFlags(intent.FLAG_ACTIVITY_INHERIT_MICHAEL);


最后就会在这里处理:


int prevTopOfHome = intentActivity.task.getTaskToReturnTo(); 1878


  1.  if ((launchFlags &  
  2.                             (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_INHERIT_MICHAEL))  
  3.                             == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_INHERIT_MICHAEL)) {  
  4.                         // Caller wants to appear on home activity.  
  5.                         intentActivity.task.setTaskToReturnTo(prevTopOfHome);  
  6. }  


          --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);


5,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();                               //开始调用Activity的生命周期方法


                     --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启动结束。


0 0