Step 17. LoaderTask.loadAndBindAllApps
这个函数定义在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
- public class LauncherModel extends BroadcastReceiver {
- ......
-
- private class LoaderTask implements Runnable {
- ......
-
- private void loadAndBindAllApps() {
- ......
-
- if (!mAllAppsLoaded) {
- loadAllAppsByBatch();
- if (mStopped) {
- return;
- }
- mAllAppsLoaded = true;
- } else {
- onlyBindAllApps();
- }
- }
-
-
- ......
- }
-
- ......
- }
由于还没有加载过应用程序,这里的mAllAppsLoaded为false,于是就继续调用loadAllAppsByBatch函数来进一步操作了。
Step 18. LoaderTask.loadAllAppsByBatch
这个函数定义在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
- public class LauncherModel extends BroadcastReceiver {
- ......
-
- private class LoaderTask implements Runnable {
- ......
-
- private void loadAllAppsByBatch() {
- ......
-
- final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-
- final PackageManager packageManager = mContext.getPackageManager();
- List<ResolveInfo> apps = null;
-
- int N = Integer.MAX_VALUE;
-
- int startIndex;
- int i=0;
- int batchSize = -1;
- while (i < N && !mStopped) {
- if (i == 0) {
- mAllAppsList.clear();
- ......
- apps = packageManager.queryIntentActivities(mainIntent, 0);
-
- ......
-
- N = apps.size();
-
- ......
-
- if (mBatchSize == 0) {
- batchSize = N;
- } else {
- batchSize = mBatchSize;
- }
-
- ......
-
- Collections.sort(apps,
- new ResolveInfo.DisplayNameComparator(packageManager));
- }
-
- startIndex = i;
- for (int j=0; i<N && j<batchSize; j++) {
-
- mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
- i++;
- }
-
- final boolean first = i <= batchSize;
- final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
- final ArrayList<ApplicationInfo> added = mAllAppsList.added;
- mAllAppsList.added = new ArrayList<ApplicationInfo>();
-
- mHandler.post(new Runnable() {
- public void run() {
- final long t = SystemClock.uptimeMillis();
- if (callbacks != null) {
- if (first) {
- callbacks.bindAllApplications(added);
- } else {
- callbacks.bindAppsAdded(added);
- }
- ......
- } else {
- ......
- }
- }
- });
-
- ......
- }
-
- ......
- }
-
- ......
- }
-
- ......
- }
函数首先构造一个CATEGORY_LAUNCHER类型的Intent:
- final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
接着从mContext变量中获得PackageManagerService的接口:
- final PackageManager packageManager = mContext.getPackageManager();
下一步就是通过这个PackageManagerService.queryIntentActivities接口来取回所有Action类型为Intent.ACTION_MAIN,并且Category类型为Intent.CATEGORY_LAUNCHER的Activity了。
我们先进入到PackageManagerService.queryIntentActivities函数中看看是如何获得这些Activity的,然后再回到这个函数中来看其余操作。
本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966530,如需转载请自行联系原作者