1
我使用的是來自newboston的這個例子,它提示我進行錄製,但在識別出我所說的內容之後,它不會更新列表視圖。Android語音識別不起作用
這是代碼。
public class MainActivity extends Activity {
private static final int RECOGNIZER_RESULT = 1234;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
Button btn_speach = (Button)findViewById(R.id.btn_speak);
btn_speach.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to search");
startActivityForResult(intent, RECOGNIZER_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RECOGNIZER_RESULT && requestCode == RESULT_OK){
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
for(int i = 0; i < matches.size(); i++){
Log.i("MainActivity", matches.get(i));
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
這裏是xml佈局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
</RelativeLayout>
我沒有得到任何錯誤。而下面的代碼也不會在LogCat中打印任何內容。
for(int i = 0; i < matches.size(); i++){
Log.i("MainActivity", matches.get(i));
}
而我測試我的Android設備上有語音識別功能。
這裏希望這個博客將幫助您在工程深入到語音識別問題http://blog.contus.com/how-to-implement-the-voice-recognition-functionality-in-android-devices/ – 2014-07-31 10:11:02