语音识别和ACTION_CALL

时间:2020-01-14 19:16:33

标签: java android android-studio speech-recognition phone-call

我的目标是实现一个识别“帮助”一词并在用户说出帮助时拨打电话号码的应用。

MainActivity:

function gitcommit
{
   [CmdletBinding()] 
   git commit -a -m "message" | Write-Verbose
   if( $LASTEXITCODE -ne 0 ){
     Write-Warning "``git commit`` failed with exit code ${LASTEXITCODE}"
     return # Return now because the commit failed
   }

   git rev-parse HEAD # This does not need to be returned because it outputs to the output stream
   git push | Write-Verbose
   if( $LASTEXITCODE -ne 0 ){
     Write-Warning "``git push`` failed with exit code ${LASTEXITCODE}"
   }
}

function main()
{

  $commitid= gitcommit
  Write-Host "Commit id is $commitid"
}

清单:

import java.util.ArrayList;
import java.util.Locale;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


public class MainActivity extends Activity {

    private TextView feedback;
    private Button goButton;
    private final int REQ_CODE_SPEECH_INPUT = 100;


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

        feedback = findViewById(R.id.feedback);
        goButton = findViewById(R.id.goButton);


        goButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Method SpeechInput is placed//
                SpeechInput();

            }
        });

    }


    /**---------------------Showing google speech input dialog-------------------------------------*/

    private void SpeechInput() {

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);

    }


    /**--------------------------Receiving speech input--------------------------------------------*/

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT: {
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if (loopInUserWords(result)) {
                        feedback.setText("help is on it's way.");
                        // method callPhone is placed//
                        callPhone();

                    } else {
                        feedback.setText("can you say that again please? ");
                    }
                    break;
                }

            }
        }
    }


    /**-------------------Checking if the user said "help".---------------------------------------**/
    // method to loop through results trying to find help
    private boolean loopInUserWords(ArrayList<String> result) {
        for (String str : result) {
            boolean isEqual = str.equals("help");
            if (isEqual) {
                return true;
            }
        }
        return false;
    }
    /**-----------------------------------places the call.----------------------------------------**/
    public void callPhone() {

        //-----------------------------intent place a call---------------------------------//
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:0546728813"));
        //---------------------asks for permission to place a call-------------------------//
        if (ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        //--------------------------------start the call------------------------------------//
        startActivity(callIntent);
    }
}

问题是,当我第一次在我的 oneplus 7 pro 上尝试该应用程序时,它运行良好,并且在第二天再次尝试它时: 1.识别器不再用英语识别,但在等我用手机设置语言讲话。 (在它等待英语单词之前) 2.现在,在我说了几次帮助之后(当它意识到我会说英​​语时),它不再打了电话。

我的手机设置是否有问题? 我真的迷路了。

谢谢。

0 个答案:

没有答案