1.修改java

package com.adakoda.voicerecognitiontest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.adakoda.voicerecognitiontest.R.id;

public class VoiceRecognitionTestActivity extends Activity {
     private static final int REQUEST_CODE = 0;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(id.button);
       
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {

                    Intent intent = new Intent(
                            RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // ACTION_WEB_SEARCH
                    intent.putExtra(
                            RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(
                            RecognizerIntent.EXTRA_PROMPT,
                            "VoiceRecognitionTest");
                   
                             startActivityForResult(intent, REQUEST_CODE);
                } catch (ActivityNotFoundException e) {
                                    Toast.makeText(VoiceRecognitionTestActivity.this,
                        "ActivityNotFoundException", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
             if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            String resultsString = "";
           
                       ArrayList<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
           
            for (int i = 0; i< results.size(); i++) {
                     resultsString += results.get(i);
            }
           
                        Toast.makeText(this, resultsString, Toast.LENGTH_LONG).show();
        }
       
        super.onActivityResult(requestCode, resultCode, data);
    }
}
2.修改xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
<Button
    android:id="@+id/button"
    android:text="Click to start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
   
</LinearLayout>
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()