Android browser Secondary Development (I) change the search engine in browser settings

Source: Internet
Author: User

Due to xxxx's browser business customization requirements, secondary development is required based on the android browser application. The following section uses the browser source code to share some problems and experiences encountered during the development process with you.
(XXXX involves information security and cannot be disclosed)
Chapter 1 modify search engines in browser settings
1. Requirements
Change the search engine in the browser settings to xxxx, Google search, and Baidu search. XXXX is selected by default.
2. analyze source code
First, find the browser setting interface code. Part of the COM. Android. browser. browserpreferencespage. Java construction interface code is in the oncreate function.
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Load the XML preferences File
Addpreferencesfromresource (R. xml. browser_preferences );
Here we can see that the interface is constructed through the xml configuration file, which is stored in RES/XML/browser_preferences.xml under the project. The search engine is defined as follows:
<Com. Android. browser. Search. searchenginepreference
Android: Key = "search_engine"
Android: Title = "@ string/pref_content_search_engine"
Android: defaultvalue = "google"
Android: Summary = "@ string/pref_content_search_engine_summary"
Android: dialogtitle = "@ string/pref_content_search_engine"/>
We continue to find the searchenginepreference. java. Here, the search engin is obtained through the static method getsearchengineinfos of the searchengines class. This method implements the following code:
Public static list <searchengineinfo> getsearchengineinfos (context ){
Arraylist <searchengineinfo> searchengineinfos = new arraylist <searchengineinfo> ();
Resources res = context. getresources ();
String [] searchengines = res. getstringarray (R. array. search_engines );
For (INT I = 0; I <searchengines. length; I ++ ){
String name = searchengines [I];
Searchengineinfo info = new searchengineinfo (context, name );
Searchengineinfos. Add (Info );
}
Return searchengineinfos;
}
When we see R. array. search_engines, we can find the data source for configuring the search engine, in RES/values/search_engines.xml. Definition:
<! --
****
* *** This file was generated by tools/get_search_engines.py
****

Each value in the string-array is the name of a value in all_search_engines.xml
-->
<Resources xmlns: xliff = "urn: Oasis: names: TC: xliff: Document: 1.2">
<String-array name = "search_engines" translatable = "false">
<Item> Google </item>
<Item> Baidu </item>
<Item> Yahoo </item>
</String-array>
</Resources>
The annotation reminds us that the item here is just a reference and the actual definition is in all_search_engines.xml. This also facilitates search engine management in different regional languages. It is defined in a file to avoid unnecessary duplicate data.
3. Modify
Now we can figure it out and modify it. First, add the definition of XXX search engine in all_search_engines.xml, and then modify the item in search_engines.xml. There are several notes in actual operations.
Take yahoo_uk as an Example
<String-array name = "yahoo_uk" translatable = "false">
<Item> Yahoo! UK & Ireland </item>
<Item> uk.yahoo.com </item>
<Item> http://uk.search.yahoo.com/favicon.ico </item>
<Item> http://uk.search.yahoo.com/search? Ei = {inputencoding} & Fr = crmas & P = {searchterms} </item>
<Item> UTF-8 </item>
<Item> http://uk-sayt.ff.search.yahoo.com/gossip-uk-sayt? Output = fxjson & command = {searchterms} </item>
</String-array>
Each searchengine defines six <item> labels, keywords, search icons, search engine addresses, encoding methods, and search prompt addresses. These values vary by region language. Therefore, different search engines may need to be defined for different regions. Here we need to add a XXX definition that supports the Chinese region.
<String-array name = "XXXX" translatable = "true">
<Item> @ string/xxxx_str </item>
<Item> xxx.xxx.com </item>
<Item> http://xxx.xxx.com/favicon.ico </item>
<Item> http: // xxx.xxx.com/search? Ei = {inputencoding} & Fr = crmas & P = {searchterms} </item>
<Item> gb2312 </item>
<Item> </item>
</String-array>

From: http://seya.iteye.com/blog/931286

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.