参考:http://blog.csdn.net/pediyasm/article/details/7987389
1.设置默认应用的原理
当设置了默认应用后,本文以UC浏览器为例子,在手机/data/system/packages.xml中就会新增如下信息:
-
<preferred-activities>
-
<item name="com.UCMobile/.main.UCMobile" match="200000" set="2">
-
<set name="com.UCMobile/.main.UCMobile" />
-
<set name="com.android.browser/.BrowserActivity" />
-
<filter>
-
<action name="android.intent.action.VIEW" />
-
<cat name="android.intent.category.DEFAULT" />
-
<scheme name="http" />
-
</filter>
-
</item>
-
</preferred-activities>
2.实现方法。有两种方法可以实现:一种是在Provision.apk中实现,一种是在PackageManagerService.java中实现。具体代码如下:
-
private void addPreferredActivityForULauncher() {
-
IntentFilter filter = new IntentFilter();
-
filter.addAction("android.intent.action.VIEW");
-
filter.addCategory("android.intent.category.DEFAULT");
-
filter.addDataScheme("http");
-
-
ComponentName preActivity = new ComponentName("com.UCMobile", "com.UCMobile.main.UCMobile");
-
ComponentName[] set = new ComponentName[] {preActivity,new ComponentName("com.android.browser", "com.android.browser.BrowserActivity")};
-
mSettings.mPreferredActivities.addFilter(
-
new PreferredActivity(filter, 0x200000, set, preActivity));
-
mSettings.writeLPr();
-
}
-
if(isFirstBoot()){
-
addPreferredActivityForULauncher();
-
}
3.通过mm命令生成services.jar,并push到手机/system/framework下。同时将uc浏览器预装进系统。恢复出厂设置即可生效。
=========================
=========================
另外一种方法,在PackageManagerService.java添加如下代码:
在构造函数结尾添加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, 0);
-
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");
-
}
-
-
-
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.editPreferredActivitiesLPw(0).addFilter(new PreferredActivity(filter, match, set, activity,true));
-
mSettings.writePackageRestrictionsLPr(0);
-
}
-
}
阅读(4082) | 评论(0) | 转发(0) |