diff --git a/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java b/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java index 1a4b5e4a0..3094afe0a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java @@ -20,6 +20,8 @@ import com.example.android.apis.R; import android.app.Activity; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; @@ -29,6 +31,7 @@ import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; +import java.util.List; /** * Sample code that invokes the speech recognition intent API. @@ -54,8 +57,16 @@ public class VoiceRecognition extends Activity implements OnClickListener { mList = (ListView) findViewById(R.id.list); - // Attach actions to buttons - speakButton.setOnClickListener(this); + // Check to see if a recognition activity is present + PackageManager pm = getPackageManager(); + List activities = pm.queryIntentActivities( + new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); + if (activities.size() != 0) { + speakButton.setOnClickListener(this); + } else { + speakButton.setEnabled(false); + speakButton.setText("Recognizer not present"); + } } /** @@ -71,7 +82,6 @@ public class VoiceRecognition extends Activity implements OnClickListener { * Fire an intent to start the speech recognition activity. */ private void startVoiceRecognitionActivity() { - //TODO Get these values from constants Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); @@ -85,6 +95,7 @@ public class VoiceRecognition extends Activity implements OnClickListener { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { + // Fill the list view with the strings the recognizer thought it could have heard ArrayList matches = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,