小编abe*_*ape的帖子

Android SearchRecentSuggestions - 在SearchView中输入时不会显示建议

我有一个工作搜索小部件,并希望添加搜索历史建议.我遵循Android教程(http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html),虽然搜索仍然有效,但没有显示任何建议.这是我的代码:

  1. 内容提供商

    package com.mypackage;
    
    import android.content.SearchRecentSuggestionsProvider;
    
    public class SearchHistoryProvider extends SearchRecentSuggestionsProvider {
        public final static String AUTHORITY = SearchHistoryProvider.class.getName();
        public final static int MODE = DATABASE_MODE_QUERIES;
    
        public SearchHistoryProvider() {
            setupSuggestions(AUTHORITY, MODE);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在Manifest中声明提供程序

    <provider 
        android:name=".SearchHistoryProvider"
        android:authorities="com.mypackage.SearchHistoryProvider">
    </provider>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 可搜索的配置

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint"
        android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
        android:searchSuggestAuthority="com.mypackage.SearchHistoryProvider"
        android:searchSuggestSelection=" ?">
    </searchable>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将查询保存到内容提供程序(在我的可搜索活动中)

    private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    
        String query = intent.getStringExtra(SearchManager.QUERY);
        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                            SearchHistoryProvider.AUTHORITY, SearchHistoryProvider.MODE);
        suggestions.saveRecentQuery(query, null);
    
        // Collapse the search view …
    Run Code Online (Sandbox Code Playgroud)

java search android search-suggestion

21
推荐指数
1
解决办法
3303
查看次数

Android搜索小部件 - onQueryTextSubmit和将Sent发送到SearchableActivity之间的区别?

我有一个包含SearchView小部件的活动.我正在使用onQueryTextSubmit侦听器处理文本搜索的结果,这很好.(活动本身被指定为可搜索活动).

我最近决定通过在searchable.xml文件中添加"voiceSearchMode"属性来添加语音识别:

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>
Run Code Online (Sandbox Code Playgroud)

当我添加语音识别时,onQueryTextSubmit侦听器在提供语音输入后不会被调用(但是,在使用editText框提供文本输入后仍会调用它).语音识别器将ACTION_SEARCH Intent发送回相同的Activity(可以在onCreate方法中处理).有没有办法用语音识别器激活onQueryTextSubmit方法(或类似的东西,不需要重新创建活动?)我问的原因是因为如果识别器必须发送一个意图,我必须创建和使用APP_DATA发送额外的捆绑包,但似乎无法正常工作.

所以我的问题是:

(1)如何使用(或可以使用)启用了语音识别搜索的onQueryTextSubmit侦听器?(与常规基于文本的搜索一样使用它)

(2)如果(1)不可能,那么如何通过意图传递带语音识别搜索查询的附加数据?我尝试通过onSearchRequested()添加它,如下所示:

@Override
public boolean onSearchRequested() {
    Bundle appData = new Bundle();
    appData.putInt("testKey", 44);
    this.startSearch(null, true, appData, false);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在onCreate中访问它时,appData为null:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.overview_list);

    Bundle extras = getIntent().getExtras();
    Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);

    // Receive search intents (from voice recognizer)
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      //doMySearch(query);
    }
}
Run Code Online (Sandbox Code Playgroud)

(另外,当我添加onSearchRequested处理程序时,按放大镜图标可以将搜索小部件扩展两次 - 我想这是因为除了设置可搜索的xml之外我手动启动搜索组态).

在相关的说明中,在同一活动中发送意图而不是使用侦听器有什么好处?我知道如果您的SearchableActivity是另一项活动,那么您可能希望向其发送意图; …

search android voice-recognition android-intent

9
推荐指数
1
解决办法
2180
查看次数