Android-home Key and launcher Startup Process Analysis

Source: Internet
Author: User

Android-home Key and launcher Startup Process Analysis launcher, which is the android desktop application. The first application started at startup and the first application entered by pressing the home key is this program. If you need to modify the startup process or create a new launcher, you need to understand the process. Part 1: system default Home application (launcher) Startup Process 1. frameworks/base/services/java/com/android/server/SystemServer. java has always remembered that the "hello world" program is to write a main function, so here we also start from the main function: public static void main (String [] args) there are two processes: SystemServer. init1 starts several important navtive services, such as SurfaceFlinger and SensorService SystemServer. init2 starts java service, such as ContentService, PowerManagerService, MountService, WindowManagerService, etc. 2. frameworks/B Ase/services/java/com/android/server/am/ActivityManagerServcie. java starts ActivityManagerService and jumps to [java] Slog. I (TAG, "Activity Manager"); context = ActivityManagerService. main (factoryTest); public static final Context main (int factoryTest) {AThread thr = new AThread (); thr. start (); synchronized (thr) {while (thr. mService = null) {try {thr. wait () ;}catch (InterruptedException e) {}} Activ ItyManagerService m = thr. mService; mSelf = m; ActivityThread at = ActivityThread. systemMain (); mSystemThread = at; Context context =. getSystemContext (); context. setTheme (android. r. style. theme_Holo); m. mContext = context; m. mFactoryTest = factoryTest; m. mMainStack = new ActivityStack (m, context, true); m. mBatteryStatsService. publish (context); m. mUsageStatsService. publish (context); synchronized (Thr) {thr. mReady = true; thr. policyall ();} m. startRunning (null, null); return context;} This function first creates an ActivityManagerService instance internally through the AThread thread object, then, the instance is saved to its member variable mService. Then, the ActivityManagerService instance is saved to the static member variable mSelf of the ActivityManagerService class, and other member variables are initialized. The startup code of the AThread thread is as follows: [java] AThread thr = new AThread (); thr. start (); static class AThread extends Thread {ActivityManagerService mService; boolean mReady = false; public AThread () {super ("ActivityManager");} public void run () {Looper. prepare (); android. OS. process. setThreadPriority (android. OS. process. THREAD_PRIORITY_FOREGROUND); android. OS. process. setCanSelfBackground (false); ActivityManagerServ Ice m = new ActivityManagerService (); synchronized (this) {mService = m; policyall () ;} synchronized (this) {while (! MReady) {try {wait ();} catch (InterruptedException e ){}}}...} 3. ActivityManagerService. systemReady ServerThread. the run function calls [java] SystemService after initializing a series of services in the system. java: // We now tell the activity manager it is okay to run third party // code. it will call back into us once it has gotten to the state // where third party code can really run (but before it has actually // started launching th E initial applications), for us to complete our // initialization. activityManagerService. self (). systemReady (new Runnable () {public void run () {Slog. I (TAG, "Making services ready"); core code: [java] public final class ActivityManagerService extends ActivityManagerNative implements Watchdog. monitor, BatteryStatsImpl. batteryCallback {...... public void systemReady (final Runnable goingCallback ){...... Synchronized (this ){...... mMainStack. resumeTopActivityLocked (null );}}......} 4. ActivityStack. resumeTopActivityLocked [java] final boolean resumeTopActivityLocked (ActivityRecord prev, Bundle options) {// Find the first activity that is not finishing. activityRecord next = topRunningActivityLocked (null); // Remember how we'll process this pause/resume situation, and ensure // that the sta Te is reset however we wind up proceeding. final boolean userLeaving = mUserLeaving; mUserLeaving = false; if (next = null) {// There are no more activities! Let's just start up the // Launcher... if (mMainStack) {ActivityOptions. abort (options); return mService. startHomeActivityLocked (mCurrentUser );}}...} here, the topRunningActivityLocked function is called to return the Activity at the top of the current system Activity stack. Because no Activity has been started yet, the return value is null, that is, the value of the next variable is null, therefore, mService is called. startHomeActivityLocked function 5, startHomeActivityLocked [java] boolean startHomeActivityLocked (int userId ){... intent intent = New Intent (mTopAction, mTopData! = Null? Uri. parse (mTopData): null); intent. setComponent (mTopComponent); if (mFactoryTest! = SystemServer. FACTORY_TEST_LOW_LEVEL) {intent. addCategory (Intent. CATEGORY_HOME);} ActivityInfo aInfo = resolveActivityInfo (intent, STOCK_PM_FLAGS, userId); if (aInfo! = Null) {intent. setComponent (new ComponentName (aInfo. applicationInfo. packageName, aInfo. name); // Don't do this if the home app is currently being // instrumented. aInfo = new ActivityInfo (aInfo); aInfo. applicationInfo = getAppInfoForUser (aInfo. applicationInfo, userId); ProcessRecord app = getProcessRecordLocked (aInfo. processName, aInfo. applicationInfo. uid); if (app = null | app. instrumentatio NClass = null) {intent. setFlags (intent. getFlags () | Intent. FLAG_ACTIVITY_NEW_TASK); mMainStack. startActivityLocked (null, intent, null, aInfo, null, null, 0, 0, 0, 0, null, false, null) ;}} return true ;} the function first creates an Intent of the CATEGORY_HOME type, and then uses the Intent. the resolveActivityInfo function queries the Activity with the Category type HOME from PackageManagerService, that is, packages/apps/Launcher2/AndroidManifest. as shown in the xml file: Send the assembled home intent. [Html] <activity android: name = "com. android. launcher2.Launcher "android: launchMode =" singleTask "android: clearTaskOnLaunch =" true "android: stateNotNeeded =" true "android: theme =" @ style/Theme "android: screenOrientation = "nosensor" android: windowSoftInputMode = "stateUnspecified | adjustPan"> <intent-filter> <action android: name = "android. intent. action. MAIN "/> <category android: name =" android. intent. category. HOME "/> <category android: name =" android. intent. category. DEFAULT "/> <category android: name =" android. intent. category. MONKEY "/> </intent-filter> </activity> 6. ResolverActivity. javaframeworks \ base \ core \ java \ com \ android \ internal \ app \ ResolverActivity. java [java]/*** This activity is displayed when the system attempts to start an Intent for * which there is more than one matching activity, allowing t He user to decide * which to go. it is not normally used directly by application developers. */public class ResolverActivity extends AlertActivity implements AdapterView. onItemClickListener {This is an application that finds and matches the intent-filter application. If there are more than one application, the list will prompt you to select the application. By default, or only one application will directly access the application. [Java] mCurrentResolveList = mPm. queryIntentActivities (mIntent, PackageManager. MATCH_DEFAULT_ONLY );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.