APP Launcher 之桌面Application启动篇

桌面launcer启动流程之application:

App 启动后随后application被第一个启动,并加装了所有的软件,以及注册了应用的增删改等广播操作。并且为主界面加入罗引用等操作。


//当整个桌面程序启动后,静态初始化,在线程里获取应用信息
static final HandlerThread sWorkerThread = new HandlerThread("LoadingApps"); 
static {
sWorkerThread.start();
}


@Override
public void onCreate() {
super.onCreate();
registerRecever();// 注册应用添加、删除等广播
}


/**
* 注册应用添加、删除等广播
*/
private void registerRecever() {
// register recevier
mLauncherReceiver = new LauncherReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);//软件被安装
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);//软件被删除
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);//安装取消
filter.addDataScheme("package");
registerReceiver(mLauncherReceiver, filter);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); //外部应用程序
filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); //过滤不可以用的程序
registerReceiver(mLauncherReceiver, filter);
filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);//局部作用改变了
registerReceiver(mLauncherReceiver, filter);
}

//注册了广播后,在这里即可监听,软件安装删除过程等操作。

private class LauncherReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG, "LauncherReceiver onRecive action = " + action);
//应用添加、改变、移除的广播
if (Intent.ACTION_PACKAGE_ADDED.equals(action)
|| Intent.ACTION_PACKAGE_CHANGED.equals(action)
|| Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
final String packageName = intent.getData()
.getSchemeSpecificPart();
final boolean replacing = intent.getBooleanExtra(
Intent.EXTRA_REPLACING, false);
Log.d(TAG, "LauncherReceiver onRecive packageName = "
+ packageName);
int op = PackageUpdatedTask.OP_NONE;
if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
op = PackageUpdatedTask.OP_UPDATE;
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
if (!replacing)
op = PackageUpdatedTask.OP_REMOVED;
} else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
if (!replacing)
op = PackageUpdatedTask.OP_ADD;
}
if (op != PackageUpdatedTask.OP_NONE) {
sWorkerHandler.post(new PackageUpdatedTask(op,
new String[] { packageName }));
}


} else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE
.equals(action)) {//SD卡应用可用的广播,有用户的应用安装到SD卡中
String[] packages = intent
.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
sWorkerHandler.post(new PackageUpdatedTask(
PackageUpdatedTask.OP_ADD, packages));
} else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE
.equals(action)) {//SD卡应用可用的广播
String[] packages = intent
.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
sWorkerHandler.post(new PackageUpdatedTask(
PackageUpdatedTask.OP_REMOVED, packages));
} else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {//系统切换语言的广播
sWorkerHandler.post(new PackageUpdatedTask(
PackageUpdatedTask.OP_RELOAD));
}
}


}
}


整个launcher的app入口初始化到这里结束。

你可能感兴趣的:(android)