Merge "Show how to use broadcast for accessing voice search settings."
This commit is contained in:
@@ -33,6 +33,26 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/speak_button" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/supported_languages" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/supported_languages"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/language_preference" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/language_preference"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<ListView android:id="@+id/list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dip"
|
||||
|
||||
@@ -307,6 +307,8 @@
|
||||
<string name="menu_from_xml_instructions_go_back">If you want to choose another menu resource, go back and re-run this activity.</string>
|
||||
|
||||
<string name="voice_recognition">App/Voice Recognition</string>
|
||||
<string name="supported_languages">Supported languages</string>
|
||||
<string name="language_preference">Voice Search in Settings</string>
|
||||
|
||||
<string name="text_to_speech">App/Text-To-Speech</string>
|
||||
<string name="again">Again</string>
|
||||
|
||||
@@ -19,16 +19,24 @@ package com.example.android.apis.app;
|
||||
import com.example.android.apis.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.speech.RecognizerIntent;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.SpinnerAdapter;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -38,16 +46,23 @@ import java.util.List;
|
||||
*/
|
||||
public class VoiceRecognition extends Activity implements OnClickListener {
|
||||
|
||||
private static final String TAG = "VoiceRecognition";
|
||||
|
||||
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
|
||||
|
||||
private ListView mList;
|
||||
|
||||
private Handler mHandler;
|
||||
|
||||
private Spinner mSupportedLanguageView;
|
||||
|
||||
/**
|
||||
* Called with the activity is first created.
|
||||
*/
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mHandler = new Handler();
|
||||
|
||||
// Inflate our UI from its XML layout description.
|
||||
setContentView(R.layout.voice_recognition);
|
||||
@@ -57,6 +72,8 @@ public class VoiceRecognition extends Activity implements OnClickListener {
|
||||
|
||||
mList = (ListView) findViewById(R.id.list);
|
||||
|
||||
mSupportedLanguageView = (Spinner) findViewById(R.id.supported_languages);
|
||||
|
||||
// Check to see if a recognition activity is present
|
||||
PackageManager pm = getPackageManager();
|
||||
List<ResolveInfo> activities = pm.queryIntentActivities(
|
||||
@@ -67,6 +84,11 @@ public class VoiceRecognition extends Activity implements OnClickListener {
|
||||
speakButton.setEnabled(false);
|
||||
speakButton.setText("Recognizer not present");
|
||||
}
|
||||
|
||||
// Most of the applications do not have to handle the voice settings. If the application
|
||||
// does not require a recognition in a specific language (i.e., different from the system
|
||||
// locale), the application does not need to read the voice settings.
|
||||
refreshVoiceSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,9 +105,29 @@ public class VoiceRecognition extends Activity implements OnClickListener {
|
||||
*/
|
||||
private void startVoiceRecognitionActivity() {
|
||||
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
|
||||
|
||||
// Specify the calling package to identify your application
|
||||
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
|
||||
|
||||
// Display an hint to the user about what he should say.
|
||||
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
|
||||
|
||||
// Given an hint to the recognizer about what the user is going to say
|
||||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
|
||||
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
|
||||
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
|
||||
|
||||
// Specify how many results you want to receive. The results will be sorted
|
||||
// where the first result is the one with higher confidence.
|
||||
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
|
||||
|
||||
// Specify the recognition language. This parameter has to be specified only if the
|
||||
// recognition has to be done in a specific language and not the default one (i.e., the
|
||||
// system locale). Most of the applications do not have to set this parameter.
|
||||
if (!mSupportedLanguageView.getSelectedItem().toString().equals("Default")) {
|
||||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
|
||||
mSupportedLanguageView.getSelectedItem().toString());
|
||||
}
|
||||
|
||||
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
|
||||
}
|
||||
|
||||
@@ -104,4 +146,85 @@ public class VoiceRecognition extends Activity implements OnClickListener {
|
||||
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
private void refreshVoiceSettings() {
|
||||
Log.i(TAG, "Sending broadcast");
|
||||
sendOrderedBroadcast(RecognizerIntent.getVoiceDetailsIntent(this), null,
|
||||
new SupportedLanguageBroadcastReceiver(), null, Activity.RESULT_OK, null, null);
|
||||
}
|
||||
|
||||
private void updateSupportedLanguages(List<String> languages) {
|
||||
// We add "Default" at the beginning of the list to simulate default language.
|
||||
languages.add(0, "Default");
|
||||
|
||||
SpinnerAdapter adapter = new ArrayAdapter<CharSequence>(this,
|
||||
android.R.layout.simple_spinner_item, languages.toArray(
|
||||
new String[languages.size()]));
|
||||
mSupportedLanguageView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private void updateLanguagePreference(String language) {
|
||||
TextView textView = (TextView) findViewById(R.id.language_preference);
|
||||
textView.setText(language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the response of the broadcast request about the recognizer supported languages.
|
||||
*
|
||||
* The receiver is required only if the application wants to do recognition in a specific
|
||||
* language.
|
||||
*/
|
||||
private class SupportedLanguageBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, final Intent intent) {
|
||||
Log.i(TAG, "Receiving broadcast " + intent);
|
||||
|
||||
final Bundle extra = getResultExtras(false);
|
||||
|
||||
if (getResultCode() != Activity.RESULT_OK) {
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
showToast("Error code:" + getResultCode());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (extra == null) {
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
showToast("No extra");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (extra.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
|
||||
mHandler.post(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
updateSupportedLanguages(extra.getStringArrayList(
|
||||
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (extra.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
|
||||
mHandler.post(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
updateLanguagePreference(
|
||||
extra.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void showToast(String text) {
|
||||
Toast.makeText(VoiceRecognition.this, text, 1000).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user