下面列出了怎么用android.content.SearchRecentSuggestionsProvider的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Although provider utility classes are typically static, this one must be constructed
* because it needs to be initialized using the same values that you provided in your
* {@link android.content.SearchRecentSuggestionsProvider}.
*
* @param authority This must match the authority that you've declared in your manifest.
* @param mode You can use mode flags here to determine certain functional aspects of your
* database. Note, this value should not change from run to run, because when it does change,
* your suggestions database may be wiped.
*
* @see android.content.SearchRecentSuggestionsProvider
* @see android.content.SearchRecentSuggestionsProvider#setupSuggestions
*/
public SearchRecentSuggestions(Context context, String authority, int mode) {
if (TextUtils.isEmpty(authority) ||
((mode & SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES) == 0)) {
throw new IllegalArgumentException();
}
// unpack mode flags
mTwoLineDisplay = (0 != (mode & SearchRecentSuggestionsProvider.DATABASE_MODE_2LINES));
// saved values
mContext = context;
mAuthority = new String(authority);
// derived values
mSuggestionsUri = Uri.parse("content://" + mAuthority + "/suggestions");
}
private void sendSearchIntent () {
try {
Intent sendIntent = new Intent(this, Class.forName(searchableActivity));
sendIntent.setAction(Intent.ACTION_SEARCH);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sendIntent.putExtra(SearchManager.QUERY, query);
// If it is set one-line mode, directly saves the suggestion in the provider
if (!CustomSearchableInfo.getIsTwoLineExhibition()) {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, providerAuthority, SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES);
suggestions.saveRecentQuery(query, null);
}
startActivity(sendIntent);
finish();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void getManifestConfig () {
try {
Map<String, String> providers = ManifestParser.getProviderNameAndAuthority(this);
OUTER: for (String key : providers.keySet()) {
providerAuthority = providers.get(key).toString();
providerName = key;
if (Class.forName(providerName).getSuperclass().equals(SearchRecentSuggestionsProvider.class)) {
isRecentSuggestionsProvider = Boolean.TRUE;
break OUTER;
} else {
isRecentSuggestionsProvider = Boolean.FALSE;
}
}
searchableActivity = ManifestParser.getSearchableActivity(this);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}