Android內搜尋引擎的改造

來源:互聯網
上載者:User

更改瀏覽器設定中的搜尋引擎
1. 需求
將瀏覽器設定中的搜尋引擎更改為XXXX, Google搜尋,百度搜尋。預設 XXXX
選中。
2. 分析源碼
我們先找到瀏覽器的設定介面代碼, com.android.browser.BrowserPreferencesPage.java 構造介面部分代碼在
onCreate 函數中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the XML preferences file
addPreferencesFromResource(R.xml.browser_preferences);
這裡我們看到它是通過xml 設定檔來構造介面的,這個檔案存放在工程下的 res/xml/browser_preferences.xml
。裡面對於 search engine 是這樣定義的:
<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" />
我們繼續順藤摸瓜,找到SearchEnginePreference.java 這裡面對於 search engin
又是通過 SearchEngines 類靜態方法 getSearchEngineInfos
來擷取的,這個方法實現如下代碼:
public static List<SearchEngineInfo> getSearchEngineInfos(Context 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;
}
看到R.array.search_engines 我們就找到配置搜尋引擎的資料來源頭了,在 res/values/search_engines.xml
中。 定義如下:
<!--
****
**** 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>
注釋中已經提醒我們,這裡的item 只是一個引用,真正的定義在 all_search_engines.xml
中。這樣也是方便不同地區語言下的搜尋引擎管理。集中到一個檔案中定義,避免不必要的重複資料。
3. 修改
這下都搞清楚了就好修改了。首先在all_search_engines.xml 中添加對 XXX
搜尋引擎的定義,然後修改 search_engines.xml 中的 item.
在實際操作中有幾點還有說明下。
以Yahoo_uk 為例
<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>
每個searchEngine 都定義了 6 個
<item> 從上到下依次表示顯示標籤,關鍵字,搜尋表徵圖,搜尋引擎地址,編碼方式,搜尋提示地址。 這些值因地區語言不同而不同。所以對不同的地區可能需要定義不同的搜尋引擎。我們這裡需要添加一個支援中國地區的
xxx 定義就可以了。
<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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.