1. Create an XML folder under the Resources folder and create a searchable.xml:
<?XML version= "1.0" encoding= "Utf-8"?><searchablexmlns:android= "Http://schemas.android.com/apk/res/android"Android:label= "@string/sms_search"Android:hint= "@string/sms_search"android:searchsuggestauthority= "Cn.itcast.smsmanager.MySuggestionProvider"android:searchsuggestselection=" ?" ></searchable>
2. Configuration files
<ActivityAndroid:name=". Searchableactivity "Android:theme= "@android: Style/theme.notitlebar" > <!--The following two nodes must have - <Intent-filter> <ActionAndroid:name= "Android.intent.action.SEARCH" /> </Intent-filter> <Meta-dataAndroid:name= "Android.app.searchable"Android:resource= "@xml/searchable" /> </Activity> <!--Specify which searchableactivity we want to activate. - <Meta-dataAndroid:name= "Android.app.default_searchable"Android:value=". Searchableactivity " />
3. Create Provider
Public classMysuggestionproviderextendsSearchrecentsuggestionsprovider { Public Final StaticString authority = "Cn.itcast.smsmanager.MySuggestionProvider"; Public Final Static intMODE =database_mode_queries; Private Final Staticstring[] Sms_projection =NewString[]{sms._id,sms.address,sms.body}; Private Final Staticstring[] ColumnNames =Newstring[]{basecolumns._id, searchmanager.suggest_column_text_1, Searchmanager.suggest_column_text_2, Searchmanager.suggest_column_query}; PublicMysuggestionprovider () {setupsuggestions (authority, MODE); } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { //TODO auto-generated Method StubLOG.I ("i", "Query ..."); if(Selectionargs! =NULL) {String query= Selectionargs[0]; if(textutils.isempty (query)) {return NULL; } Uri Uri1=Sms.content_uri; String where= Sms.body + "like '%" + query + "% '"; Cursor Cursor= GetContext (). Getcontentresolver (). Query (Uri1, Sms_projection, where,NULL, sms.date + "desc"); returnchangecursor (cursor); } return NULL; } Privatecursor changecursor (cursor cursor) {matrixcursor result=Newmatrixcursor (ColumnNames); if(Cursor! =NULL){ while(Cursor.movetonext ()) {object[] columnvalues=Newobject[]{cursor.getstring (Cursor.getcolumnindex (sms._id)), cursor.getstring (cursor.getcolumnin Dex (sms.address)), Cursor.getstring (Cursor.getcolumnindex (sms.body)), cursor . GetString (Cursor.getcolumnindex (sms.body))}; Result.addrow (columnvalues); } } returnresult; }}
4. Activity to store search results:
Public classSearchableactivityextendslistactivity {PrivateListView Mlistview; PrivateQueryhandler Mqueryhandler; PrivateSearchadapter Madapter; protected voidonCreate (Android.os.Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Intent Intent=getintent (); Mlistview=Getlistview (); Madapter=NewSearchadapter ( This,NULL); Mlistview.setadapter (Madapter); Mlistview.setbackgroundcolor (Color.White); if(Intent.ACTION_SEARCH.equals (Intent.getaction ())) {String query=Intent.getstringextra (searchmanager.query); Dosearch (query); } mlistview.setonitemclicklistener (NewMyonitemclicklistener ()); }}
Http://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865610.html