Android语音识别DEMO

public class VoiceActivity extends Activity implements OnClickListener {
    private static final int CODE = 11;
    private Button voice_btn;
    private ListView voice_lv;
    private List resList;
    private ArrayAdapter mAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_layout);
        initPackages();
        initViewsAndEvents();
    }

    private void initPackages() {
        PackageManager pm = getPackageManager();
        Intent resIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        resList = pm.queryIntentActivities(resIntent, 0);
    }

    private void initViewsAndEvents() {
        this.voice_btn = (Button) findViewById(R.id.voice_btn);
        this.voice_lv = (ListView) findViewById(R.id.voice_lv);
        this.voice_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        if (null == resList || resList.size() == 0) {
            Toast.makeText(this, "暂无应用支持该功能", Toast.LENGTH_SHORT).show();
            return;
        }
        voiceRecognize();

    };

    private void voiceRecognize() {
        // 用Intent来传递语音识别的模式,并且开启语音模式
        Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // 语言模式和自由形式的语音识别
        in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        // 提示语言开始
        in.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start Speaking...");
        startActivityForResult(in, CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_OK && requestCode == CODE) {
            // 取得语音的字符
            ArrayList result = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            // ListView显示数据
            mAdapter = new ArrayAdapter(this,
                    android.R.layout.simple_list_item_1, result);
            voice_lv.setAdapter(mAdapter);
        }
    }
}

<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" >

    <TextView
        android:id="@+id/voice_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="left|center"
        android:text="点击下面按钮开始声音识别"
        android:textSize="15sp" />

    <Button
        android:id="@+id/voice_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="开始"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/voice_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#dedede"
        android:dividerHeight="1px"
        android:scrollbars="none" >
    ListView>

LinearLayout>

你可能感兴趣的:(安卓学习)