短信内容搜索2-Search-搜索(搜索提示,搜索建议)
搜索提示(建议)步骤: 1、创建一个内容提供者 ——Creating a Content Provider ①写一个内容提供者类继承SearchRecentSuggestionsProvider类 public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "cn.zengfansheng.sms.provider.MySuggestionProvider";
// public final static int MODE = DATABASE_MODE_QUERIES;// 1
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;// 两行,可选
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
} ②在清单文件中注册内容提供者 <provider
android:name="cn.zengfansheng.sms.provider.MySuggestionProvider"
android:authorities="cn.zengfansheng.sms.provider.MySuggestionProvider" >
</provider> ③并在自定义的内容提供者的无参构造函数中调用setupSuggestions()方法 2、searchable.xml文件中配置 ——Modifying the Searchable Configuration ① android:searchSuggestAuthority="cn.zengfansheng.sms.provider.MySuggestionProvider" 和1中所写的内容提供者的authorities一致 ② android:searchSuggestSelection=" ?" SQL语句的一个占位符selection,自动替换用户所输入的 <?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name"
android:searchSuggestAuthority="cn.zengfansheng.sms.provider.MySuggestionProvider"
android:searchSuggestSelection=" ?" >
</searchable> 3、重写query()方法 其中 selectionArgs[0]就是搜索的关键字 4、 将smsCursor转换为一个MatrixCursor BaseColumns._ID,//_id
SearchManager.SUGGEST_COLUMN_TEXT_1,// suggest_text_1
SearchManager.SUGGEST_COLUMN_TEXT_2, // suggest_text_2
SearchManager.SUGGEST_COLUMN_QUERY /**
* 搜索提示内容提供者
* @author hacket
*
*/
public class MySuggestionProvider extends SearchRecentSuggestionsProvider { private static final String TAG = "MySuggestionProvider";
public final static String AUTHORITY = "cn.zengfansheng.sms.provider.MySuggestionProvider";
// public final static int MODE = DATABASE_MODE_QUERIES;// 1
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;// 两行,可选
private final String[] SEARCH_PROJECTION = { "_id", "address", /*"date",*/"body" };
private final int ID_COLUMN_INDEX = 0;
private final int ADDRESS_COLUMN_INDEX = 1;
private final int BODY_COLUMN_INDEX = 2;
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String query = selectionArgs[0];
Log.e(TAG, "查询的内容是:" + query);
// 1、拿到关键字进行短信内容数据的模糊匹配查询
String where = "body like ?";
String[] whereArgs = { "%" + query + "%" };
ContentResolver resolver = getContext().getContentResolver();
Cursor smsCursor = resolver.query(Sms.SMS_URI, SEARCH_PROJECTION, where, whereArgs, "date desc");
// Utils.printCursor(smsCursor);
// 2、将smsCursor转换为一个MatrixCursor
String[] columnNames = new String[] {
BaseColumns._ID,//_id
SearchManager.SUGGEST_COLUMN_TEXT_1,// suggest_text_1
SearchManager.SUGGEST_COLUMN_TEXT_2, // suggest_text_2
SearchManager.SUGGEST_COLUMN_QUERY
};
MatrixCursor matrixCursor = new MatrixCursor(columnNames);
if (smsCursor != null && smsCursor.getCount() > 0) {
Object[] columnValues = new Object[columnNames.length];
while (smsCursor.moveToNext()) {
columnValues[ID_COLUMN_INDEX] = smsCursor.getInt(ID_COLUMN_INDEX);
columnValues[ADDRESS_COLUMN_INDEX] = smsCursor.getString(ADDRESS_COLUMN_INDEX);
columnValues[BODY_COLUMN_INDEX] = smsCursor.getString(BODY_COLUMN_INDEX);
// 这个值是给搜索结果页面展示获取的值-intent.getStringExtra(SearchManager.QUERY);
columnValues[3] = smsCursor.getString(BODY_COLUMN_INDEX);
matrixCursor.addRow(columnValues);
}
smsCursor.close();
}
return matrixCursor;
}
}
|
|
|