AI 147251: am: CL 147247 am: CL 147243 Make the API sample check to see if there is anything installed to handle the recognition intent.
Original author: jham
Merged from: //branches/cupcake/...
Original author: android-build
Automated import of CL 147251
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 1a4b5e4..3094afe 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 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.ListView;
import java.util.ArrayList;
+import java.util.List;
/**
* Sample code that invokes the speech recognition intent API.
@@ -54,8 +57,16 @@
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<ResolveInfo> 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 @@
* 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 @@
@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<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,