Browser — 更换bookmark、homepage及常见问题

Browser的开发作业通常是:更换bookmark(书签)、homepage(预置主页)

bookmark(书签)

在.xml中列举出bookmark的列表,替换的网址都放这里面

    <add-resource type="array" name="bookmark_url"></add-resource>
    <string-array name="bookmark_url">
        <item>Google</item>
        <item>http://www.google.com/</item>
    </string-array>

在.java逻辑中,这里面resId_DefaultUrl的值与add-resource、string-array中的name值相同

            //获取.xml中的资源
            Resources res = getContext().getResources();
            //获取机种ID
            String carrierId = com.android.browser.Browser.getCarrier();
            //通过拼接的方式拼接机种ID来识别bookmark的网址URL
            int resId_DefaultUrl = res.getIdentifier("bookmark_url_" + carrierId, "array", getContext().getPackageName());

homepage(预置主页)

在.xml中列举出主页网址

    <add-resource type="string" name="homepage_url"></add-resource>
    <string name="homepage_url"   translatable="false">https://www.google.com/</string>

在.java逻辑中,思路和bookmark相同

//获取机种ID
String carrierId = com.android.browser.Browser.getCarrier();
//通过拼接的方式拼接机种ID来识别homepage的网址URL
int resId_DefaultUrl = mContext.getResources().getIdentifier("homepage_url_" + carrierId, "string", mContext.getPackageName());

开发中可能遇到的问题

1.Android P修改浏览器默认主页

其中CID为手机ID,source = android-home是当前环境为安卓环境

    <!-- The default homepage. -->
    <string name="homepage_base" translatable="false">
        https://www.google.com/webhp?client={CID}&amp;source=android-home</string>

Android 10以后

    <!-- The default homepage. -->
    <string name="homepage_base" translatable="false">
        https://www.baidu.com/</string>

2.你使用的URL是https://www.google.com/

但是你会发现实际访问网址的URL是https://www.google.com.hk/

这是因为Google的服务器搬离了中国大陆,大陆地区用户使用google服务时会自动跳转到香港的https://www.google.com.hk/,有关键字过滤,偶尔不稳定

解决方案:使用https://www.google.com/ncr

ncr是no country redirection,强制不跳转的命令

3.网址URL尽量写规范https://www.google.com/

之前我写的是https://www.google.com,因为少了个/,网址不规范,导致有的手机硬件识别不出来这个网址

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>