android 当系统存在多个Launcher时,如何设置开机自动进入默认的Launcher

下面以设置Google Launcher2作为默认启动的Launcher为例,其包名为com.android.launcher2
编辑packages/apps/Provision/src/com/android/DefaultActivity.java文件
......
// remove this activity from the package manager.
PackageManager pm = getPackageManager();

//add start
Intent intent=new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(intent, 0);
if(resolveInfoList != null){
int size = resolveInfoList.size();
for(int j=0;j<size;j++){
final ResolveInfo r = resolveInfoList.get(j);
if(r.activityInfo.packageName.equals(this.getPackageName())){
resolveInfoList.remove(j);
size -= 1;
break;
}
}

ComponentName[] set = new ComponentName[size];
ComponentName defaultLauncher=new ComponentName("com.android.launcher2", "com.android.launcher2.Launcher");
int defaultMatch=0;
for(int i=0;i<size;i++){
final ResolveInfo resolveInfo = resolveInfoList.get(i);
Log.d(TAG, resolveInfo.toString());
set[i] = new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name);
if(defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)){
defaultMatch = resolveInfo.match;
}
}
Log.d(TAG,"defaultMatch="+Integer.toHexString(defaultMatch));
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
pm.clearPackagePreferredActivities(defaultLauncher.getPackageName());
pm.addPreferredActivity(filter, defaultMatch, set, defaultLauncher);
}
//add end

// remove this activity from the package manager.
ComponentName name = new ComponentName(this, DefaultActivity.class);
pm.setComponentEnabledSetting(name,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
......

然后修改packages/apps/Provision/AndroidManifest.xml文件,增加以下语句:
<uses-permission android:name=”android.permission.SET_PREFERRED_APPLICATIONS” />

备注:上面所添加到代码,一定要放在pm.setComponentEnabledSetting这两行语句之前,如果放在这个语句后面,那后面的代码就无法执行了,导致设置实效。
这里的com.android.launcher2是Google默认的Launcher2,可以根据自己的需要来定制,让系统进入指定的那个Launcher。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值