應用中調用系統的搜索UI,Android Search Framework的初步瞭解

經過以下幾個部分的實現和配置,Android內建的搜索框架就可以在你的應用中方便使用了。

1 在需要顯示search ui界面的activity中調用search的代碼
  1.     /** Handle "search" title-bar action. */
  2.     public void onSearchClick(View v) {
  3.             onSearchRequested();
  4.     }
  5.     
  6.     /** do the search **/
  7.     @Override
  8.     public boolean onSearchRequested() {   
  9.         Bundle appDataBundle = new Bundle();   
  10.         appDataBundle.putString("param1", "test");   
  11.         startSearch("請輸入搜索的關鍵詞", true, appDataBundle, false);   
  12.         return true;   
  13.     }
複製代碼

2 創建xml/searchable.xml 對search的配置
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3.     Copyright 2011 xxx Inc.
  4. -->
  5. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  6.     android:label="@string/search_label"
  7.     android:hint="@string/search_hint"
  8.     <!-- Authority設置成對應provider的全稱 -->
  9. android:searchSuggestAuthority="com.xxx.alimobile.util.SearchSuggestionsProvider"
  10.     android:searchSuggestIntentAction="android.intent.action.SEARCH"
  11.     android:searchSuggestThreshold="0"
  12.     android:searchSuggestSelection=" ? " />
複製代碼
3 在AndroidManifest.xml中,給處理Search請求的頁面加入intent過濾器和searchable配置文件
  1.      <activity
  2.                     android:name=".ui.WebViewActivity"
  3.                     android:theme="@style/Theme.AliMobile"
  4.                     android:screenOrientation="portrait"
  5.                     android:configChanges="orientation|keyboard|keyboardHidden">
  6.                     <intent-filter>
  7.                             <action android:name="android.intent.action.SEARCH" />
  8.                     </intent-filter>
  9.                     <meta-data 
  10.                     android:name="android.app.searchable" 
  11.                         android:resource="@xml/searchable" />
  12.             </activity>
複製代碼
4 判斷是不是action_search intent事件發起的,然後處理。
  1. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {   
  2.             String queryString = intent.getStringExtra(SearchManager.QUERY);
  3.             strUrl = "http://search1.wap.taobao.com/s/search.htm?q=" + queryString;
  4.             }
複製代碼

後面還剩下suggestion的配置, 參考此篇技術文章:
http://www.apkbus.com/android-14572-1-1.html
5 SearchSuggestionProvider類,繼承自android.content.SearchRecentSuggestionsProvider,直接使用sdk中的簡單實現。
  1. import android.content.SearchRecentSuggestionsProvider;

  2. public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
  3.         public final static String AUTHORITY = "com.xxx.alimobile.util.SearchSuggestionsProvider";
  4.         public final static int MODE = DATABASE_MODE_QUERIES;
  5.         
  6.         public SearchSuggestionsProvider() {
  7.                 setupSuggestions(AUTHORITY, MODE);
  8.         }
  9. }
複製代碼

6 在第4步基礎上加入保存功能,每次使用搜索後調用provider進行保存搜索的字段。
  1.      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {   
  2.             String queryString = intent.getStringExtra(SearchManager.QUERY);
  3.             strUrl = "http://search1.wap.taobao.com/s/search.htm?q=" + queryString;
  4.             // SAVE THE SEARCH QUERY
  5.             SearchRecentSuggestions suggestions = 
  6.                     new SearchRecentSuggestions(
  7.                             this, SearchSuggestionsProvider.AUTHORITY, 
  8.                             SearchSuggestionsProvider.MODE);
  9.             suggestions.saveRecentQuery(queryString, null);
  10.             } else {
  11.                     strUrl = (String) intent.getExtras().get("goUrl");
  12.             }
複製代碼
7 AndroidManifest.xml中加入provider聲明。
  1.   <provider 
  2.                         android:name=".util.SearchSuggestionsProvider"
  3.                         android:authorities="com.xxx.alimobile.util.SearchSuggestionsProvider">
  4.                 </provider>
複製代碼


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章