Java 如何结束倾听行动

Java 如何结束倾听行动,java,android,pepper,Java,Android,Pepper,我正在努力理解如何,如果可能的话,在Pepper上的监听操作开始后结束它。我希望在我的应用程序中实现以下目标: Pepper向用户提问,用户可以通过平板电脑上的语音或触摸输入回答 如果答案是否定的,Pepper将不得不执行动画,同时用短语回答 这是处理语音输入的代码部分,它按预期工作: public void onRobotFocusGained(QiContext qiContext) { ... //Pepper greets the approach

我正在努力理解如何,如果可能的话,在Pepper上的监听操作开始后结束它。我希望在我的应用程序中实现以下目标:

  • Pepper向用户提问,用户可以通过平板电脑上的语音或触摸输入回答
  • 如果答案是否定的,Pepper将不得不执行动画,同时用短语回答
这是处理语音输入的代码部分,它按预期工作:

public void onRobotFocusGained(QiContext qiContext) {
         ...

        //Pepper greets the approached user
        animation_future = greeting_animation.async().run();
        //After the animation is finished, wait for human input
        animation_future.andThenConsume(chatting ->{
            listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
            listen.andThenConsume(heardPhrase->{
                //Pepper start listening
                result = heardPhrase.run();
                //If the response contains "yes"
                if( (result.getHeardPhrase().getText().toLowerCase()).equals("si")){
                    //User need helps, Pepper start discussing with it
                    helpNeeded_chat.async().run();
                    //Otherwise
                }else if( (result.getHeardPhrase().getText().toLowerCase()).equals("no")){
                    //No help is required, Pepper says goodbye
                    animation_future = goodbye_animation.async().run();
                    //A new user comes by - Restart scenario
                    animation_future.andThenConsume(restart->{
                        Log.i(TAG, "Interaction ended. Restarting.");
                        //Restart by starting this same activity
                        startActivity(new Intent(this, MainActivity.class));
                    });
                }
            });
        });
    }

虽然这是处理触摸输入的部分(在OnRobotFocus之外的自己的方法中定义):

在这种情况下,由于Listen操作一直在运行,因此会抛出警告Pepper在侦听时不能讲话,从而阻止任务的正确结束。 我发现,唯一允许终止操作的方法是requestCancellation(),但在我的情况下似乎不起作用,用于检查操作是否终止的布尔方法isCancelled()总是返回False


实际上可以停止侦听操作,或者我必须完全改变代码的结构方式(即从一开始就使用聊天机器人)?

当您调用
Listen.requestCancellation()
时,实际上您正在取消侦听操作的构建。 您使用以下命令定义了
listen
listen=ListenBuilder.with(qiContext).withPhraseSet(response.buildAsync()

相反,您应该取消运行操作时返回的future:
result=heardPhrase.run()。
调用
result.requestCancellation()
应该可以帮到你


经验法则是先构建Qi SDK操作,然后再运行。

当您调用
listen.requestCancellation()
时,实际上您正在取消构建listen操作。 您使用以下命令定义了
listen
listen=ListenBuilder.with(qiContext).withPhraseSet(response.buildAsync()

相反,您应该取消运行操作时返回的future:
result=heardPhrase.run()。
调用
result.requestCancellation()
应该可以帮到你

经验法则是先构建Qi SDK操作,然后运行

final Button button_no = findViewById(R.id.button_no);
button_no.setOnClickListener(v->{
    ...
    listen.requestCancellation();
    //Help is refused - Restart scenario
    animation_future = goodbye_animation.async().run();
    animation_future.andThenConsume(restart->{
        Log.i(TAG, "Interaction ended. Restarting.");
        //Restart by starting this same activity
        startActivity(new Intent(this, MainActivity.class));
    });
});