应用中调用系统的搜索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>
复制代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值