描述:
当系统存在多个浏览器时,如何预置某个浏览器为系统默认的浏览器?
方法:
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("packageName ","activityName");
//find all browsers
List
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 (packageName.equals("packageName")) no_UC = false;
ComponentName componentName = new ComponentName(packageName, className);
arrayOfComponentName[i] = componentName;
}
if (no_UC){
return;
}
//set default
addPreferredActivity2(filter,IntentFilter.MATCH_CATEGORY_SCHEME, arrayOfComponentName,component);
}
3.增加addPreferredActivity2接口(还是在PackageManagerService.java中):
public void addPreferredActivity2(IntentFilter filter, int match,
ComponentName[] set, ComponentName activity) {
// writer
synchronized (mPackages) {
Slog.i(TAG, "Adding preferred activity " + activity + ":");
filter.dump(new LogPrinter(Log.INFO, TAG), " ");
mSettings.editPreferredActivitiesLPw(0).addFilter(
new PreferredActivity(filter, match, set, activity));
mSettings.writePackageRestrictionsLPr(0);
}
}