如何预置某个浏览器为系统默认的浏览器?

[DESCRIPTION]
当系统存在多个浏览器时,如何预置某个浏览器为系统默认的浏览器?
 
 
 
[SOLUTION]

以设定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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值