当android 系统启动后,系统回去启动 Launcher.java 程序。Launcher.java 也是一个activity。
在onCreate() 方法中,调用方法
if (!mRestoring) {
startLoaders();
}
startLoaders();
}
private void startLoaders() {
boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
mRestoring = false;
}
boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
mRestoring = false;
}
在startLoaders()方法中,调用了LauncherModel 中的方法loadApplications(...)方法。
在loadApplications(...)方法中执行了下面的代码。
if (!isLaunching) {
startApplicationsLoaderLocked(launcher, false);
return false;
}
startApplicationsLoaderLocked(launcher, false);
return false;
}
在startApplicationsLoaderLocked(launcher, false);
private void startApplicationsLoaderLocked(Launcher launcher, boolean isLaunching) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> starting applications loader");
stopAndWaitForApplicationsLoader();
mApplicationsLoader = new ApplicationsLoader(launcher, isLaunching);
mApplicationsLoaderThread = new Thread(mApplicationsLoader, "Applications Loader");
mApplicationsLoaderThread.start();
}
if (DEBUG_LOADERS) d(LOG_TAG, " --> starting applications loader");
stopAndWaitForApplicationsLoader();
mApplicationsLoader = new ApplicationsLoader(launcher, isLaunching);
mApplicationsLoaderThread = new Thread(mApplicationsLoader, "Applications Loader");
mApplicationsLoaderThread.start();
}
在此方法中启动了一个mApplicationsLoader 的线程。
public void
run() {
if (DEBUG_LOADERS) d(LOG_TAG, " ----> running applications loader (" + mId + ")");
// Elevate priority when Home launches for the first time to avoid
// starving at boot time. Staring at a blank home is not cool.
android.os.Process.setThreadPriority(mIsLaunching ? Process.THREAD_PRIORITY_DEFAULT :
Process.THREAD_PRIORITY_BACKGROUND);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final Launcher launcher = mLauncher.get();
final PackageManager manager = launcher.getPackageManager();
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
run() {
if (DEBUG_LOADERS) d(LOG_TAG, " ----> running applications loader (" + mId + ")");
// Elevate priority when Home launches for the first time to avoid
// starving at boot time. Staring at a blank home is not cool.
android.os.Process.setThreadPriority(mIsLaunching ? Process.THREAD_PRIORITY_DEFAULT :
Process.THREAD_PRIORITY_BACKGROUND);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final Launcher launcher = mLauncher.get();
final PackageManager manager = launcher.getPackageManager();
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
在这个方法中,主要对PackageManager 对象进行了初始化,并且通过此对象来加载系统中的所有的intentActivity.
所有在android中没有main方法。并且在AndroidManifest.xml 文件中在确定第一个启动的类似,这个类只能是Activity。
此时所有的系统中安装的应用程序就已经被加载完成了。
当用户点击显示界面中的应用时:
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof ApplicationInfo) {
// Open shortcut
final Intent intent = ((ApplicationInfo) tag).intent;
startActivitySafely(intent);
} else if (tag instanceof FolderInfo) {
handleFolderClick((FolderInfo) tag);
}
}
此时就会获得用户点击的intent,然后调用 startActivitySafely(...)启动应用程序。
void startActivitySafely(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
". Make sure to create a MAIN intent-filter for the corresponding activity " +
"or use the exported attribute for this activity.", e);
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
". Make sure to create a MAIN intent-filter for the corresponding activity " +
"or use the exported attribute for this activity.", e);
}
}