SearchView语音识别器不会转录文本

时间:2014-08-30 16:35:22

标签: android voice searchview

我设置了一个简单的Android项目,其中包含一个包含启用了语音搜索的SearchView的活动。当我使用语音搜索功能时,内置的语音识别器会按预期显示,但它不会在识别器对话框或SearchView小部件本身中转录语音文本。但是,它通过SEARCH意图正确发送语音文本,因此可以执行搜索。这是我的项目的源代码和XML:

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="searchtest.screens"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name="searchtest.screens.MainScreen"
        android:label="@string/app_name"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable_deal" />
    </activity>
</application>

</manifest>

searchable_deal.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_deals_hint"
android:label="@string/app_name"
android:voiceLanguageModel="web_search"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable>

main_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dealScreenLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/searchLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <SearchView
        android:id="@+id/dealSearch"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="@string/search_deals_hint" >
    </SearchView>
</LinearLayout>

<ListView
    android:id="@+id/dealList"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight=".8" >
</ListView>
</LinearLayout>

MainScreen.java

public class MainScreen extends Activity implements OnItemClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    setupSearchView();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // use an inflater to populate the ActionBar with items
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.view_deals, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return true;
}

@Override
protected void onNewIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        String query = intent.getStringExtra(SearchManager.QUERY);
        doSearch(query);
    }
}

private void setupSearchView() {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) findViewById(R.id.dealSearch);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(searchableInfo);
    searchView.setIconifiedByDefault(false);
}

private void doSearch(String query) {

    Toast.makeText(this, "Searching for: " + query, Toast.LENGTH_LONG).show();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Toast.makeText(this, "Selected item: " + position, Toast.LENGTH_LONG).show();
}
}

2 个答案:

答案 0 :(得分:0)

请阅读以下注释:

  

注意:仔细考虑语音搜索是否适合您的   应用。使用语音搜索按钮执行的所有搜索都是   立即发送到您的搜索活动,没有机会   用户查看转录的查询。充分测试声音   识别并确保它理解查询的类型   用户可以在您的申请中提交。

http://developer.android.com/guide/topics/search/search-dialog.html#VoiceSearch

答案 1 :(得分:0)

那么也许您可以在收到意图时自己设置文本并在onQueryTextChange中进行实际搜索。