Browser的开发作业通常是:更换bookmark(书签)、homepage(预置主页)
bookmark(书签)
在.xml中列举出bookmark的列表,替换的网址都放这里面
- <code> <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值相同
- <code> //获取.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中列举出主页网址
- <code> <add-resource type="string" name="homepage_url"></add-resource>
- <string name="homepage_url" translatable="false">https://www.google.com/</string>
复制代码
在.java逻辑中,思路和bookmark相同
- <code>//获取机种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是当前环境为安卓环境
- <code> <add-resource type="string" name="homepage_url"></add-resource>
- <string name="homepage_url" translatable="false">https://www.google.com/</string>webhp?client={CID}&source=android-home
复制代码
Android 10以后
- <code> <!-- 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,因为少了个/,网址不规范,导致有的手机硬件识别不出来这个网址
来源:https://blog.caogenba.net/m0_50408097/article/details/122433824
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |