• android VoiceRecognition 语音识别并打印到列表上


    package com.example.wenandroid;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    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.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    
    public class VoiceRecognizeDemo extends Activity implements OnClickListener {
    private static final int VOICE_RECOGNITION_REQUEST_CODE=1234;
    private ListView listview;
    private Button btn;
    
    	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.voicerecogenize);
    	listview=(ListView)findViewById(R.id.listview);
    	btn=(Button)findViewById(R.id.btn);
    	PackageManager pm=getPackageManager();
    	List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            btn.setOnClickListener(this);
        } else {
            btn.setEnabled(false);
            btn.setText("Recognizer not present");
        }
    
    }
    
    	@Override
    	public void onClick(View v) {
    		if(v.getId()==R.id.btn){
    			startVoiceRecognitionActivity();
    		}
    
    	}
    
    	private void startVoiceRecognitionActivity(){
    		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    	}
    
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
    	            // Fill the list view with the strings the recognizer thought it could have heard
    	            ArrayList<String> matches = data.getStringArrayListExtra(
    	                    RecognizerIntent.EXTRA_RESULTS);
    	            listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
    	                    matches));
    	        }
    
    		super.onActivityResult(requestCode, resultCode, data);
    	}
    	
    }
    


    界面只有一个listview和一个button。

  • 相关阅读:
    同一页面的不同Iframe获取数据
    同一页面的两个Iframe获取数据
    关于从SVN检出项目后,项目名称还是之前修改之前或者项目名称不对问题
    <fieldset>标签
    利用js动态创建<style>
    找换硬币问题 与 0-1背包问题区别
    某种 找换硬币问题的贪心算法的正确性证明
    部分背包问题的贪心算法正确性证明
    从 活动选择问题 看动态规划和贪心算法的区别与联系
    求解两个字符串的最长公共子序列
  • 原文地址:https://www.cnblogs.com/pangblog/p/3275518.html
Copyright © 2020-2023  润新知