android应用程序的启动过程

当android 系统启动后,系统回去启动 Launcher.java 程序。Launcher.java 也是一个activity。
在onCreate() 方法中,调用方法

        if (!mRestoring) {
            startLoaders();
        }

    private void startLoaders() {
        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);

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

在此方法中启动了一个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);

在这个方法中,主要对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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值