当系统存在多个launcher时,若没有设置默认launcher,开机启动后会弹出提示框,罗列所有launcher,用户选择并设置了默认launcher后,按home键以及以后重启都会进入默认的launcher。
现在,我希望系统能直接就进入我设定的launcher而不是弹出框后选择然后设置
网上大部分的做法就是修改
packages/apps/Provision/src/com/android/DefaultActivity.java
和
frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
两个文件,给个相对比较好看一点的链接http://blog.csdn.net/z_guijin/article/details/8964890
我按照这个做不能达到预期的效果,
/////////////////////////////////////////添加内容////////////////////////////////////////
后期修改包名,发现开机自启动出现问题,然后弄了两天,终于差不多弄清楚了。果然,欠下的债迟早得还!
其实修改DefaultActivity.java就能够完成开机自启动,只是当时我的程序有问题,所以没有达到预期效果
当然下面修改ActivityManagerService.java也能完成开机自启动
这两者的区别在于
DefaultActivity.java只是在第一次启动时执行,如果修改了默认launcher后不可恢复
ActivityManagerService.java在每次启动时执行,每次都默认启动设定的launcher,当然,如果设定的launcher存在的话,设置其他的launcher为默认会无效,因为重新启动时setDefaultLauncher()会将当前默认launcher清除。只有在卸载了设定默认启动的launcher后才能设置其他launcher为默认启动.
//////////////////////////////////////////////////////////////////////////////////////////////////////////
修改多次搜索关键字,得到另一篇文章,大致看了下,感觉不会有效,但是已经绝望了就试了试,竟然解决了问题http://blog.csdn.net/jia4525036/article/details/18036765
这篇文章有借鉴之处,直接使用时会发现有些字段是上下文中没有的,故写下此文记录一下。
修改文件
frameworks\base\services\java\com\android\server\am\ActivityManagerService.java
添加一个方法:
private void setDefaultLauncher() {
// get default component
String packageName = "com.coship.factorytest";//默认launcher包名
String className = "com.coship.factorytest.MainActivity";////默认launcher入口
IPackageManager pm = ActivityThread.getPackageManager();
//判断指定的launcher是否存在
if(hasApkInstalled(packageName)) {
Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
//清除当前默认launcher
ArrayList intentList = new ArrayList();
ArrayList cnList = new ArrayList();
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 list = new ArrayList();
try {
list = pm.queryIntentActivities(intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), PackageManager.MATCH_DEFAULT_ONLY);
}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;
}
}
try {
pm.addPreferredActivity(filter, defaultMatch, set, launcher);
} catch (RemoteException e) {
throw new RuntimeException("com.coship.factorytest.MainActivity : Package manager has died", e);
}
}//end if
}
private static boolean hasApkInstalled(String pkgname) {
try {
mSelf.mContext.getPackageManager().getPackageInfo(pkgname,0);
} catch(Exception e) {
Slog.d(TAG, "PackageManager.NameNotFoundException: = " + e.getMessage());
return false;
}
return true;
}
然后在ActivityManagerService类中的
boolean startHomeActivityLocked()
方法第一行调用上面添加的
setDefaultLauncher()
boolean startHomeActivityLocked() {
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();
///////////////////////////////////////////
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 =
intent.resolveActivityInfo(mContext.getPackageManager(),
STOCK_PM_FLAGS);
if (aInfo != null) {
intent.setComponent(new ComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
Proce***ecord app = getProce***ecordLocked(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, null, 0, aInfo,
null, null, 0, 0, 0, false, false, null);
}
}
return true;
}
添加后的方法全部内容如上,重新编译android,烧录,开机就能够自动进入自定义的launcher
可以通过系统设置取消该launcher的默认设置,取消之后按home键会弹出launcher选择提示框
frameworks\base\core\java\com\android\internal\app\ResolverActivity.java
ResolverActivity类就是选择打开方式的弹出框