需求:使用自己的应用作为launcher应用
想要替换桌面launcher,首先你自己的应用中要有home属性
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
- 路径: /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
private void setDefaultLauncher(){
// 替换应用的包名
String defaultlauncherpckname = "com.xxx.xxx.xxxx";
PackageManager mPm = mContext.getPackageManager();
Intent mIntent = new Intent();
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> mList = mPm.queryIntentActivities(mIntent, 0);
ComponentName[] mHomeComponentSet = new ComponentName[mList.size()];
ComponentName newHome = null;
for (int i = 0; i < mList.size(); i++) {
ActivityInfo info = mList.get(i).activityInfo;
ComponentName componentName = new ComponentName(info.packageName, info.name);
mHomeComponentSet[i] = componentName;
if (info.packageName.equals(defaultlauncherpckname)) {
newHome = componentName;
}
}
if (newHome != null) {
IntentFilter mHomeFilter = new IntentFilter();
mHomeFilter.addAction(Intent.ACTION_MAIN);
mHomeFilter.addCategory(Intent.CATEGORY_HOME);
mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY, mHomeComponentSet, newHome);
}
}
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
// We are running in factory test mode, but unable to find
// the factory test app, so just sit around displaying the
// error message and don't try to start anything.
return false;
}
// set default launcher
setDefaultLauncher();
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}