我该如何推出自己的Andr​​oid活动与语音识别? [英] How can I launch my Android activities with the speech recognizer?

查看:117
本文介绍了我该如何推出自己的Andr​​oid活动与语音识别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变这个开关,这样,而不是点击按钮我的活动将由讲有关水果的名称推出。例如,苹果类将通过说出字苹果启动。我应该如何改写这个开关?所有我在做这个尝试没有似乎迄今工作。所提供的任何答案将大大AP preciated。谢谢!

 包com.example.speech;进口com.example.speech.R;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;公共类MainActivity扩展活动实现OnClickListener {    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //发现我们的按钮,并为所有onClickListeners
        的ImageButton 1 =(的ImageButton)findViewById(R.id.one);
        的ImageButton 2 =(的ImageButton)findViewById(R.id.two);
        的ImageButton 3 =(的ImageButton)findViewById(R.id.three);
        的ImageButton 4 =(的ImageButton)findViewById(R.id.four);
        的ImageButton 5 =(的ImageButton)findViewById(R.id.five);
        的ImageButton 6 =(的ImageButton)findViewById(R.id.six);
        one.setOnClickListener(本);
        two.setOnClickListener(本);
        three.setOnClickListener(本);
        four.setOnClickListener(本);
        five.setOnClickListener(本);
        six.setOnClickListener(本);
    }    公共无效的onClick(视图v){        //做基础上被点击的按钮的东西
        开关(v.getId()){
            案例R.id.one:
                //创建一个意图,指示我们要
                //启动活性。                            意图I =新意图(这一点,Apple.class);
                //基础上,意图启动活动
                startActivity(ⅰ);
                完();
                打破;            案例R.id.two:
                    意向J =新意图(这一点,Orange.class);                    //基础上,意图启动活动
                    startActivity(J);
                    完();
                    打破;            案例R.id.three:
                    意图K =新意图(这一点,Banana.class);                    //基础上,意图启动活动
                    startActivity(K);
                    完();
                    打破;            案例R.id.four:
                    目的L =新意图(这一点,Grape.class);                    //基础上,意图启动活动
                    startActivity(升);
                    完();
                    打破;            案例R.id.five:
                    意图M =新意图(这一点,Strawberry.class);                    //基础上,意图启动活动
                    startActivityForResult(M,0);
                    完();
                    打破;            案例R.id.six:
                    意图N =新意图(这一点,Kiwi.class);                    //基础上,意图启动活动
                    startActivity(N);
                    完();
                    打破;
                    默认:
                    完();
        }    };
}

这是处理语音活动结果的正确方法是什么?

  @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == VOICE_RECOGNITION_REQUEST_ code)            //如果语音识别成功,则它返回RESULT_OK
            如果(结果code == RESULT_OK){                ArrayList的<串GT; textMatchList =数据
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);                如果(!textMatchList.isEmpty()){
                    //如果第一场比赛包含单词苹果
                    //然后启动苹果的活动。
                    如果(textMatchList.get(0)。载有(苹果)){
                            //创建一个意图,指示我们要
                //启动活性。                            意图I =新意图(这一点,Apple.class);
                //基础上,意图启动活动
                startActivity(ⅰ);
                完();
                打破;

我觉得事情是这行后失踪......

 如果(textMatchList.get(0)。载有(苹果)){


解决方案

这里是关于如何使用Android的语音识别的样本教程。你需要处理的onActivityResult搜索结果,做一个字符串匹配以苹果,香蕉,葡萄等..如果字符串匹配,推出各自的活动!

I want to alter this switch such that instead of clicking the buttons my activities will launch by speaking the name of the associated fruit. For instance, the Apple class will launch by speaking the word "Apple". How should I rewrite this switch? All of my attempts at doing this have not seemed to work thus far. Any answers provided will be greatly appreciated. Thank you!

package com.example.speech;

import com.example.speech.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // find our buttons and set onClickListeners for all
        ImageButton one = (ImageButton)findViewById(R.id.one);
        ImageButton two = (ImageButton)findViewById(R.id.two);
        ImageButton three = (ImageButton)findViewById(R.id.three);
        ImageButton four = (ImageButton)findViewById(R.id.four);
        ImageButton five = (ImageButton)findViewById(R.id.five);
        ImageButton six = (ImageButton)findViewById(R.id.six);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
    }

    public void onClick(View v) {

        // do something based on the button that was clicked
        switch(v.getId()) {
            case R.id.one:
                // create an intent indicating we want
                // to start the activity.

                            Intent i = new Intent(this, Apple.class);
                // start the activity based on the Intent
                startActivity(i);
                finish();
                break;

            case R.id.two:
                    Intent j = new Intent(this, Orange.class);

                    // start the activity based on the Intent
                    startActivity(j);
                    finish();
                    break;

            case R.id.three:
                    Intent k = new Intent(this, Banana.class);

                    // start the activity based on the Intent
                    startActivity(k);
                    finish();
                    break;

            case R.id.four:
                    Intent l = new Intent(this, Grape.class);

                    // start the activity based on the Intent
                    startActivity(l);
                    finish();
                    break;

            case R.id.five:
                    Intent m = new Intent(this, Strawberry.class);

                    // start the activity based on the Intent
                    startActivityForResult(m, 0);
                    finish();
                    break;

            case R.id.six:
                    Intent n = new Intent(this, Kiwi.class);

                    // start the activity based on the Intent
                    startActivity(n);
                    finish();
                    break;
                    default:
                    finish();
        }

    };
}

Is this the correct way to handle the voice activity result?

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

            //If Voice recognition is successful then it returns RESULT_OK
            if(resultCode == RESULT_OK) {

                ArrayList<String> textMatchList = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (!textMatchList.isEmpty()) {
                    // If first Match contains the word 'apple'
                    // Then start the apple activity.
                    if (textMatchList.get(0).contains("apple")) {
                            // create an intent indicating we want
                // to start the activity.

                            Intent i = new Intent(this, Apple.class);
                // start the activity based on the Intent
                startActivity(i);
                finish();
                break;

I feel like something is missing after this line...

if (textMatchList.get(0).contains("apple")) {

解决方案

Here is a sample tutorial on how to use Android voice recognition. You need to handle the search result in OnActivityResult and do a string match to apple, banana,grape and so on.. if the string match is found, launch the respective activity!

这篇关于我该如何推出自己的Andr​​oid活动与语音识别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆