Google Voice Search in Android:
Activity Class:
package com.example.myvoicerecognition;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
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.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
public class VoiceRecognition extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private EditText m_etTextHint;
private ListView m_lvTextMatches;
private Spinner m_spTextMatches;
private Button m_btnSpeak;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_recognition);
m_etTextHint = (EditText) findViewById(R.id.vr_etTextHint);
m_lvTextMatches = (ListView) findViewById(R.id.vr_lvTextMatches);
m_spTextMatches = (Spinner) findViewById(R.id.vr_spNoOfMatches);
m_btnSpeak = (Button) findViewById(R.id.vr_btnSpeak);
}
public void checkVoiceRecognition() {
PackageManager mPackageManger = getPackageManager();
List<ResolveInfo> mActivities = mPackageManger.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (mActivities.size() == 0) {
m_btnSpeak.setEnabled(false);
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
public void speak(View mView) {
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, m_etTextHint.getText()
.toString());
// Given an hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
// If number of Matches is not selected then return show toast message
if (m_spTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
Toast.makeText(this, "Please select No. of Matches from spinner",
Toast.LENGTH_SHORT).show();
return;
}
int m_noOfMatches = Integer.parseInt(m_spTextMatches.getSelectedItem()
.toString());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, m_noOfMatches);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
// If Voice recognition is successful then it returns RESULT_OK
if (resultCode == RESULT_OK) {
ArrayList<String> mTextMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (mTextMatchList.isEmpty()) {
// If first Match contains the 'search' word
// Then start web search
if (mTextMatchList.get(0).contains("search")) {
String m_searchQuery = mTextMatchList.get(0).replace(
"search", " ");
Intent m_searchIntent = new Intent(
Intent.ACTION_WEB_SEARCH);
m_searchIntent.putExtra(SearchManager.QUERY,
m_searchQuery);
startActivity(m_searchIntent);
} else {
// populate the Matches
m_lvTextMatches.setAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,
mTextMatchList));
}
}
// Result code for various error.
} else if (resultCode == RecognizerIntent.RESULT_AUDIO_ERROR) {
showToastMessage("Audio Error");
} else if (resultCode == RecognizerIntent.RESULT_CLIENT_ERROR) {
showToastMessage("Client Error");
} else if (resultCode == RecognizerIntent.RESULT_NETWORK_ERROR) {
showToastMessage("Network Error");
} else if (resultCode == RecognizerIntent.RESULT_NO_MATCH) {
showToastMessage("No Match");
} else if (resultCode == RecognizerIntent.RESULT_SERVER_ERROR) {
showToastMessage("Server Error");
}
super.onActivityResult(requestCode, resultCode, data);
}
void showToastMessage(String p_message) {
Toast.makeText(this, p_message, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.voice_recognition, menu);
return true;
}
}
Xml Class:
<LinearLayout 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"
android:orientation="vertical" >
<EditText
android:id="@+id/vr_etTextHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="@string/etSearchHint"
android:inputType="textMultiLine"
android:lines="1" />
<Button
android:id="@+id/vr_btnSpeak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="speak"
android:padding="20dp"
android:text="@string/btSpeak"
tools:context=".VoiceRecognitionActivity" />
<Spinner
android:id="@+id/vr_spNoOfMatches"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/saNoOfMatches"
android:prompt="@string/sNoOfMatches" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvTextMatches"
android:textStyle="bold" />
<ListView
android:id="@+id/vr_lvTextMatches"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Permission:
<uses-permission android:name="android.permission.INTERNET" />
String.xml
<string name="title_activity_voice_recognition">Voice Recognition</string>
<string name="tvTextMatches">Text Matches</string>
<string name="sNoOfMatches">No of Matches</string>
<string name="etSearchHint">Speech hint here</string>
<string name="btSpeak">Speak</string>
<string-array name="saNoOfMatches">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
Thanks.
ReplyDeletedotzooapps