【Launcher2源码解读】Launcher启动和加载

本文介绍了Launcher作为系统软件的角色,当按下home键时启动。它通过在Activity中设置特定的intent-filter类别成为Launcher应用。文章详细阐述了Launcher的启动流程,从读取自定义配置文件开始,不断更新DB中的信息。主要关注点在于LauncherModel中的LoaderTask如何加载数据库信息,以及Launcher.java如何实现回调获取数据。
摘要由CSDN通过智能技术生成

Launcher是一个特殊的App,属于系统软件,在按home键时会启动的App,在你的Activity中加入如下intent-fliter 的category之后就会被系统当作Launcher应用。

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

一般我们在给视图绑定数据的时候会把它写在主线程onCreate中,如果需要加载时间,我们会用线程去辅助加载数据,Launcher启动时需要加载好App,shortcut,Folder等一系列的item,所以Launcher采用了Runable来加载数据,并且定义了一个规范的LauncherModel.Callback接口来定义具体的加载过程,步骤。

Launcher本身就是一个框架,从首次启动读取自定义的配置文件开始,操作的过程不断的更新db中存储的信息。下面来简单介绍启动流程。


那么具体到代码是如何启动并绑定数据的呢?

首先定义了接口:LauncherModel.Callback  看名字就知道是什么意思了。

public interface Callbacks {
		public boolean setLoadOnResume();

		public int getCurrentWorkspaceScreen();

		public void startBinding();

		public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);

		public void bindFolders(HashMap<Long, FolderInfo> folders);

		public void finishBindingItems();

		public void bindAppWidget(LauncherAppWidgetInfo info);

		public void bindAllApplications(ArrayList<ApplicationInfo> apps);

		public void bindAppsAdded(ArrayList<ApplicationInfo> apps);

		public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);

		public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);

		public void bindPackagesUpdated();

		public boolean isAllAppsVisible();

		public boolean isAllAppsButtonRank(int rank);

		public void bindSearchablesChanged();

		public void onPageBoundSynchronously(int page);
	}

该类中还定义了一个线程:该类就负责读书default_workspace.xml文件,读取数据库等其他的耗时操作。

private class LoaderTask implements Runnable {}


LauncherProvider.java 中定义数据库的类:

private static class DatabaseHelper extends SQLiteOpenHelper {}


其中读取文件的方法,其中用XmlPull解析相应的节点,这里还用到了Android本身读取节点属性的TypeArray来快速读取节点属性,该方法在LauncherMidel中的loadworkspace调用。如此以来,我们就可以在桌面上预制像shortcut一样的自定义图标了,铜鼓预制图标去完成一些功能。读完之后并写在了db中。

private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
			Intent intent = new Intent(Intent.ACTION_MAIN, null);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			ContentValues values = new ContentValues();

			PackageManager packageManager = mContext.getPackageManager();
			int allAppsButtonRank = mContext.getResources().getInteger(
					R.integer.hotseat_all_apps_index);
			int i = 0;
			try {
				XmlResourceParser parser = mContext.getResources().getXml(
						workspaceResourceId);
				AttributeSet attrs = Xml.asAttributeSet(parser);
				beginDocument(parser, TAG_FAVORITES);

				final int depth = parser.getDepth();

				int type;
				while (((type = parser.next()) != XmlPullParser.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值