Android的搜索小工具 - 为SearchableActivity onQueryTextSubmit和发送意图之间的区别? [英] Android Search widget - difference between onQueryTextSubmit and sending Intent to SearchableActivity?

查看:175
本文介绍了Android的搜索小工具 - 为SearchableActivity onQueryTextSubmit和发送意图之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含搜索查看小部件的活动。我处理使用onQueryTextSubmit监听文本搜索的结果,而这个工作正常。 (活动本身被指定为检索的活动)。

I have an activity that contains a SearchView widget. I am handling the results of the text search using an onQueryTextSubmit listener, and this works fine. (The activity itself is designated as a Searchable Activity).

我最近决定增加语音识别,加入了searchable.xml文件中的voiceSearchMode属性:

I recently decided to add voice recognition, by adding the "voiceSearchMode" attribute in the searchable.xml file:

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>

当我加入了语音识别功能,该onQueryTextSubmit听者没有得到提供语音输入(但是,它仍然被使用EDITTEXT盒提供文本输入之后调用)后调用。语音识别器发送一个ACTION_SEARCH意图回相同的活性(它可以在onCreate方法处理)。有没有一种方法来激活onQueryTextSubmit方法与语音识别(或类似的东西,它不需要重新创建活动?)我问的原因是因为如果识别已发出的意图,我要创建和增派同捆APP_DATA和似乎并不奏效。

When I added the voice recognition, the onQueryTextSubmit listener does not get called after providing the voice input (however, it still gets called after providing the text input using the editText box). The voice recognizer sends an ACTION_SEARCH Intent back to the same Activity (which can be handled in the onCreate method). Is there a way to activate the onQueryTextSubmit method with the voice recognizer (or something similar which doesn't require re-creating the activity?) The reason I am asking is because if the recognizer has to send an intent, I have to create and send an additional bundle with APP_DATA and that doesn't seem to be working.

所以我的问题是:

(1)你如何使用(或者可以使用)的onQueryTextSubmit监听器与语音识别搜索启用?(你会与常规的基于文本的搜索使用方法相同)

(1) How do you use (or can you use) an onQueryTextSubmit listener with voice recognition search enabled? (the same way you would use it with regular text based search)

(2)(1)是不可能的,那么我怎么能传递更多的数据通过一个意图语音识别搜索查询我试图通过onSearchRequested()这样添加它:<? / p>

(2) If (1) is not possible, then how can I pass additional data with the voice recognition search query via an intent? I tried adding it through onSearchRequested() like this:

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

但是当我尝试访问此在的onCreate,应用程序数据为空:

but when I try to access this in onCreate, appData is 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);
    }
}

(此外,当我加入onSearchRequested处理器,pressing放大镜图标对彼此顶部扩大搜索窗口小部件两次的效果 - 我想这是因为我除了手动启动搜索已经建立了一个可搜索的XML配置)。

(Also, when I add the onSearchRequested handler, pressing the magnifying glass icon has the effect of expanding the search widget twice on top of each other - I imagine this is because I am starting the search manually in addition to having set up a searchable xml configuration).

在一个相关的说明,什么是在发送使用同一活动中的监听意图的优势在哪里?我明白,如果你SearchableActivity是另一个活动,那么你会想意图发送给它;但在该SearchableActivity是包含搜索插件的同一活动的情况下,究竟是使用意图的地步?

On a related note, what is the advantage of sending an intent over using a listener within the same activity? I understand that if your SearchableActivity is another activity then you would want to send an intent to it; but in the case where the SearchableActivity is the same activity that contains the search widget, what is the point of using an intent?

任何意见和建议,将大大AP preciated。让我知道如果我需要提供更多详情。

Any comments and suggestions would be greatly appreciated. Let me know if I need to provide any additional details.

推荐答案

(1)据我可以通过广泛的调试onQueryTextSubmit不会被调用时说通过我的声音识别按钮I输入搜索查询。然而,有一个简单的解决方法 - 请参阅下面的

(1) As far as I can tell through extensive debugging onQueryTextSubmit never gets called when I input search queries through my voice recognizer button. However, there's an easy workaround - see below.

(2)我解决我的问题,通过设置活动启动模式为singleTop - 这意味着,而不是重新创建语音搜索,新ACTION_SEARCH意图活动在现有的实例中处理后的活动onNewIntent()处理。因此,您可以访问所有的现有活动的私有成员,而你并不需要通过修改搜索意图通过捆绑的任何数据。

(2) I solved my issue by setting the activity launch mode to "singleTop" - this means that instead of re-creating the activity after voice search, the new ACTION_SEARCH intent is handled within the existing instance of the activity in the onNewIntent() handler. As a result, you have access to all of the private members of the existing activity, and you don't need to pass any data through bundles by modifying the search intent.

的Andr​​oidManifest.xml :添加launchmode = singleTop属性的搜索活动:

AndroidManifest.xml: add launchmode=singleTop attribute to your searchable activity:

<activity
    android:name=".SearchableActivity"
    android:label="@string/app_name"
    android:uiOptions="splitActionBarWhenNarrow"
    android:launchMode="singleTop">

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

SearchableActivity ,加入onNewIntent()方法:

In SearchableActivity, add onNewIntent() method:

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // Gets the search query from the voice recognizer intent
        String query = intent.getStringExtra(SearchManager.QUERY);

        // Set the search box text to the received query and submit the search
        mSearchView.setQuery(query, true);
    }
}

这实质上接收语音识别查询,并将其在文本框中,并提交这是由onQueryTextSubmit像往常一样处理的文本框中搜索。

This essentially receives the voice recognizer query and puts it in the text box, and submits the text box search which is handled by onQueryTextSubmit as usual.

这篇关于Android的搜索小工具 - 为SearchableActivity onQueryTextSubmit和发送意图之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆