Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Intent;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.ResolveInfo;
     25 import android.os.Bundle;
     26 import android.speech.RecognizerIntent;
     27 import android.view.View;
     28 import android.view.View.OnClickListener;
     29 import android.widget.ArrayAdapter;
     30 import android.widget.Button;
     31 import android.widget.ListView;
     32 
     33 import java.util.ArrayList;
     34 import java.util.List;
     35 
     36 /**
     37  * Sample code that invokes the speech recognition intent API.
     38  */
     39 public class VoiceRecognition extends Activity implements OnClickListener {
     40 
     41     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
     42 
     43     private ListView mList;
     44 
     45     /**
     46      * Called with the activity is first created.
     47      */
     48     @Override
     49     public void onCreate(Bundle savedInstanceState) {
     50         super.onCreate(savedInstanceState);
     51 
     52         // Inflate our UI from its XML layout description.
     53         setContentView(R.layout.voice_recognition);
     54 
     55         // Get display items for later interaction
     56         Button speakButton = (Button) findViewById(R.id.btn_speak);
     57 
     58         mList = (ListView) findViewById(R.id.list);
     59 
     60         // Check to see if a recognition activity is present
     61         PackageManager pm = getPackageManager();
     62         List<ResolveInfo> activities = pm.queryIntentActivities(
     63                 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
     64         if (activities.size() != 0) {
     65             speakButton.setOnClickListener(this);
     66         } else {
     67             speakButton.setEnabled(false);
     68             speakButton.setText("Recognizer not present");
     69         }
     70     }
     71 
     72     /**
     73      * Handle the click on the start recognition button.
     74      */
     75     public void onClick(View v) {
     76         if (v.getId() == R.id.btn_speak) {
     77             startVoiceRecognitionActivity();
     78         }
     79     }
     80 
     81     /**
     82      * Fire an intent to start the speech recognition activity.
     83      */
     84     private void startVoiceRecognitionActivity() {
     85         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     86         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
     87                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
     88         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
     89         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
     90     }
     91 
     92     /**
     93      * Handle the results from the recognition activity.
     94      */
     95     @Override
     96     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     97         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
     98             // Fill the list view with the strings the recognizer thought it could have heard
     99             ArrayList<String> matches = data.getStringArrayListExtra(
    100                     RecognizerIntent.EXTRA_RESULTS);
    101             mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
    102                     matches));
    103         }
    104 
    105         super.onActivityResult(requestCode, resultCode, data);
    106     }
    107 }
    108