所添加的Launcher是个apk,将apk编译进去。然后参考了 http://blog.csdn.net/jia4525036/article/details/18036765后进行了一些修改。
将代码贴出来,如下:
所修改文件
frameworks\base\services\java\com\android\server\am\ActivityManagerService.java
添加一个方法:
<pre class="java" name="code"> private void setDefaultLauncher(int userId) {
String packageName = “com.android.test”; //默认launcher包名
String className = “com.android.test.MainActivity”; //默认launcher入口
Slog.i(TAG, “defautl packageName = ” + packageName + “, default className = ” + className);
IPackageManager pm = ActivityThread.getPackageManager();
//清除当前默认launcher
ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();
ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();
mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
IntentFilter dhIF = null;
for(int i = 0; i < cnList.size(); i++) {
dhIF = intentList.get(i);
if(dhIF.hasAction(Intent.ACTION_MAIN) && dhIF.hasCategory(Intent.CATEGORY_HOME)) {
mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
}
}
//获取所有launcher activity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> list = new ArrayList<ResolveInfo>();
try {
list = pm.queryIntentActivities(intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()),
PackageManager.MATCH_DEFAULT_ONLY, userId);
}catch (RemoteException e) {
throw new RuntimeException(“Package manager has died”, e);
}
// get all components and the best match
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
final int N = list.size();
Slog.d(TAG, “N:::::hyhyhyhy:::: = ” + N);
//设置默认launcher
ComponentName launcher = new ComponentName(packageName, className);
ComponentName[] set = new ComponentName[N];
int defaultMatch = 0;
for (int i = 0; i < N; i++) {
ResolveInfo r = list.get(i);
set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
Slog.d(TAG, “r.activityInfo.packageName:::::hyhyhyhy:::: = ” + r.activityInfo.packageName);
Slog.d(TAG, “r.activityInfo.name:::::hyhyhyhy:::: = ” + r.activityInfo.name);
if(launcher.getClassName().equals(r.activityInfo.name)) {
defaultMatch = r.match;
}
}
//将设置的默认launcher,添加到系统偏好
try {
pm.addPreferredActivity(filter, defaultMatch, set, launcher, userId);
} catch (RemoteException e) {
throw new RuntimeException(“factorytest.MainActivity : Package manager has died”, e);
}
}
然后在ActivityManagerService类中的boolean startHomeActivityLocked(int userId) 方法调用上面添加的 setDefaultLauncher(int userId),如下:
boolean startHomeActivityLocked(int userId) {
if (mHeadless) {
// Added because none of the other calls to ensureBootCompleted seem to fire
// when running headless.
ensureBootCompleted();
return false;
}
if (mFactoryTest == SystemServer.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;
}
///
setDefaultLauncher(userId);
///
Intent intent = new Intent(
mTopAction,
mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
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);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mMainStack.startActivityLocked(null, intent, null, aInfo,
null, null, 0, 0, 0, 0, null, false, null);
}
}
return true;
}
在原来文章中的代码里加入了一个参数userId,否则在kk版本中是会报错的。
取消该launcher的默认设置后按home键会弹出launcher选择提示框,该提示框在下述文件中
frameworks\base\core\java\com\android\internal\app\ResolverActivity.java
此问题已解决,写下来记录一下,备用。
p.s. 经测试,目前代码中并未进行第一次启动判断,故仍然存在问题,如果手动将launcher默认其它选项后,重新开机仍然是我们代码中默认的这个launcher.后在PackageManagerService.java中进行修改,已解决。