以设定UC浏览器为默认浏览器为例:
设计思路:第一次开机默认选择UC浏览器来打开网页,而且用户也是可以清除该默认选择的浏览器的,如果用户清除默认的浏览器设置,那么我们也保留用户的选择,直到reset机器。
具体方法:
1.在PackageManagerService.java中的构造函数结尾添加:setDefaultBrowser();
2.setDefaultBrowser()的具体实现:
private final void setDefaultBrowser(){
Log.d("debug_default","setDefaultBrowser is called.");
if(!isFirstBoot()) {
Log.d("debug_default","not first boot, return");
return;
}
String str1 = "android.intent.category.DEFAULT";
String str2 = "android.intent.category.BROWSABLE";
String str3 = "android.intent.action.VIEW";
//intent filter
IntentFilter filter = new IntentFilter(str3);
filter.addCategory(str1);
filter.addCategory(str2);
filter.addDataScheme("http");
//intent
Intent intent = new Intent(str3);
intent.addCategory(str2);
intent.addCategory(str1);
Uri uri = Uri.parse("http://");
intent.setDataAndType(uri, null);
//set UC,if your want set other browser as default, modify below code, add package name and activiy name
ComponentName component = new ComponentName("com.UCMobile","com.UCMobile.main.UCMobile");
//find all browsers
List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,intent.getType(),PackageManager.GET_INTENT_FILTERS);
int size = resolveInfoList.size();
ComponentName[] arrayOfComponentName = new ComponentName[size];
//make sure the default browser in phone
boolean no_UC = true;
for (int i = 0; i < size; i++)
{
ActivityInfo activityInfo = resolveInfoList.get(i).activityInfo;
String packageName = activityInfo.packageName;
String className = activityInfo.name;
//if your want set other browser as default, modify below package name
if (packageName.equals("com.UCMobile")) no_UC = false;
ComponentName componentName = new ComponentName(packageName, className);
arrayOfComponentName[i] = componentName;
}
if (no_UC){
Log.d("debug_default","no uc package");
return;
}else{
Log.d("debug_default","uc package is in, set it as Preferred.");
}
//set default
addPreferredActivity2(filter,IntentFilter.MATCH_CATEGORY_SCHEME, arrayOfComponentName,component);
Log.d("debug_default","add UC to PreferredActivity");
}
3.增加addPreferredActivity2接口(还是在PackageManagerService.java中):
public void addPreferredActivity2(IntentFilter filter, int match,
ComponentName[] set, ComponentName activity) {
Log.d("debug_default","addPreferredActivity2 is called.");
// writer
synchronized (mPackages) {
Slog.i(TAG, "Adding preferred activity " + activity + ":");
filter.dump(new LogPrinter(Log.INFO, TAG), " ");
mSettings.mPreferredActivities.addFilter(
new PreferredActivity(filter, match, set, activity));
scheduleWriteSettingsLocked();
}
}
上面是设置UC 为默认浏览器的方法,如果要设置其他浏览器为系统默认,需要修改两个地方:
1.ComponentName component = new ComponentName("packageName ","activityName");
2.if (packageName.equals("packageName")) no_UC = false;
其它都不变。
packageName代表要设置为默认浏览器的那个browser对应的包名,activityName代表要设置为默认浏览器的那个浏览器对应的类名。
红色标注的API,在android新版本上由于支持multi-user的缘故,所以接口定义发生改变,有两处需要修改(上面红色标记的语句):
1, 把List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,intent.getType(),PackageManager.GET_INTENT_FILTERS);
修改成:
List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,intent.getType(),PackageManager.GET_INTENT_FILTERS, 0);
2,把mSettings.mPreferredActivities.addFilter(
new PreferredActivity(filter, match, set, activity));
scheduleWriteSettingsLocked();
修改成
mSettings.editPreferredActivitiesLPw(0).addFilter(
new PreferredActivity(filter, match, set, activity));
mSettings.writePackageRestrictionsLPr(0);