Java 如何将最近的查询添加到其他活动

Java 如何将最近的查询添加到其他活动,java,android,Java,Android,我正在创建可搜索词典,我想显示用户在SearchView中对其他活动所做的最新查询。就像android字典中最近的选项一样 private void handleIntent(Intent inent) { if (Intent.ACTION_VIEW.equals(intent.getAction())) { // handles a click on a search suggestion; launches activity to show word In

我正在创建可搜索词典,我想显示用户在SearchView中对其他活动所做的最新查询。就像android字典中最近的选项一样

private void handleIntent(Intent inent) {
 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        Intent wordIntent = new Intent(this, WordActivity.class);
        wordIntent.setData(intent.getData());
        startActivity(wordIntent);
        finish();
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this, MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
        suggestions.saveRecentQuery(query, null);
        showResults(query);
    }
}

ShowResult显示搜索活动中的查询建议,我想将最近的查询显示到其他活动中,我如何才能做到这一点?

可能这段代码将帮助您共享首选项

SharedPreferences sPreferences = getSharedPreferences("tushar", Context.MODE_PRIVATE);
Editor sEditor = sPreferences.edit();

Set<String> setName = new HashSet<String>();
setName.add("tushar");
setName.add("pandey");
setName.add("Ram");
setName.add("Shiva");

sEditor.putStringSet("ko", setName);
sEditor.commit() ;

Log.i("hello","tushar:pandey") ;
Set<String> setName_ = sPreferences.getStringSet("tushar", setName);
Log.i(setName_.toString(),"tushar:pandey") ;
SharedReferences sPreferences=getSharedReferences(“tushar”,Context.MODE\u PRIVATE);
Editor sEditor=sPreferences.edit();
Set setName=new HashSet();
setName.add(“tushar”);
setName.add(“潘迪”);
setName.add(“Ram”);
添加(“湿婆”);
sEditor.putStringSet(“ko”,setName);
提交();
Log.i(“你好”,“图萨:潘迪”);
Set setName_u2;=sPreferences.getStringSet(“tushar”,setName);
Log.i(setName_u.toString(),“tushar:pandey”);

可以通过ContentResolver访问保存的查询:

ContentResolver contentResolver = getApplicationContext().getContentResolver();

String contentUri = "content://" + MySuggestionProvider.AUTHORITY + '/' + SearchManager.SUGGEST_URI_PATH_QUERY;

Uri uri = Uri.parse(uriStr);

Cursor cursor = contentResolver.query(uri, null, null, new String[] { null }, null);

现在,使用接收到的光标填充ListView。使用showResults()代码创建另一个活动。

我认为,通过在数据库中创建另一个表或使用共享首选项,可以将这些建议存储在表或共享首选项中。