簡體   English   中英

Android搜索小部件 - onQueryTextSubmit和將Sent發送到SearchableActivity之間的區別?

[英]Android Search widget - difference between onQueryTextSubmit and sending Intent to 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>

當我添加語音識別時,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;
}

但是當我嘗試在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);
    }
}

(另外,當我添加onSearchRequested處理程序時,按放大鏡圖標可以將搜索小部件擴展兩次 - 我想這是因為除了設置可搜索的xml之外我手動啟動搜索組態)。

在相關的說明中,在同一活動中發送意圖而不是使用偵聽器有什么好處? 我知道如果您的SearchableActivity是另一項活動,那么您可能希望向其發送意圖; 但是在SearchableActivity與包含搜索小部件的活動相同的情況下,使用意圖的重點是什么?

任何意見和建議將不勝感激。 如果我需要提供任何其他詳細信息,請與我們聯系。

(1)據我所知,通過廣泛調試onQueryTextSubmit,當我通過語音識別器按鈕輸入搜索查詢時,永遠不會調用它。 但是,有一個簡單的解決方法 - 見下文。

(2)我通過將活動啟動模式設置為“singleTop”解決了我的問題 - 這意味着在語音搜索之后不是重新創建活動,而是在onNewIntent()中的活動的現有實例內處理新的ACTION_SEARCH意圖。處理程序。 因此,您可以訪問現有活動的所有私有成員,並且無需通過修改搜索意圖來通過包傳遞任何數據。

AndroidManifest.xml :將launchmode = singleTop屬性添加到您的可搜索活動中:

<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()方法:

@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處理的文本框搜索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM