java - 如何从 View 类重复另一个类中的 Activity

标签 java android android-activity android-view

我需要从我当前的 View 类中重复另一个类中的 Activity 。

我正在开发一个简单的语音控制迷宫游戏应用程序。我需要在当前 View 类的另一个类(语音类)中开始语音识别 Activity 。语音类接受用户的语音输入,将其保存在一个变量中并返回给 View 类。 View 根据从语音类获得的输入进行更改。这必须重复,直到光标到达迷宫中的最终位置。

如何从 View 类中重复此语音 Activity ?具有新更改的 View 必须在再次开始 Activity 之前显示一小段时间。

这是我必须重复的功能。使用上下文从此函数调用语音 Activity 。

    public boolean voice_input() {
    int result;
    result = 0;
    boolean moved = false;
    try{

          Thread.sleep(1000);
    }
    catch(InterruptedException ex){

    }
    Context context = getContext();
    Intent i = new Intent(context, voice.class);
    ((Activity)context).startActivityForResult(i,requestCode);
    result = v.getVariable();
    switch(result) {
        case 1:
            moved = maze.move(Maze.UP);
            break;
        case 2:
            moved = maze.move(Maze.DOWN);
            break;
        case 3:
            moved = maze.move(Maze.RIGHT);
            break;
        case 4:
            moved = maze.move(Maze.LEFT);
            break;  
    }
    result = 0;
    if(moved) {

        invalidate();

            if(maze.isGameComplete()) {
                showFinishDialog();
            }
    }
    return true;
}

这是语音课中必须重复的 Activity 。

     public void startVoiceRecognitionActivity()
    {      
     match = 0;
     final int REQUEST_CODE = 1234;
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     startActivityForResult(intent, REQUEST_CODE);
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        match = 0;
        if (matches.contains("left")) {
            match = 4;
        }
        if (matches.contains("right")) {
            match = 3;
        }
        if (matches.contains("up")) {
            match = 1;
        }
        if (matches.contains("down")) {
            match = 2;
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
    finish();
}

最佳答案

如果我没理解错的话,解决这个问题的最佳方法是将语音识别作为一项服务来运行。看thisthis .

关于java - 如何从 View 类重复另一个类中的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501660/

相关文章:

Java如何在另一个类中使用 Activity 方法

java - 按照 JPA 规范进行类型转换

android - 如何在android中的两个 Activity 之间传输数据

android - 在 View 上下文中获取 Activity 对象

android - 从 VideoView 录制视频

java - 从 Firebase 获取特定值(value)

c# - 老大说 "We need an IPhone app",类似iPhone上的Firefox、JQuery、模板、数据链接的框架有哪些?

java - 从 DataInputStream 读取几个 byte[]

java - 如何与java代码并行读取 Parquet 文件

java - 我应该如何将文件加载到我的 Java 应用程序中?