Android API21:未调用VoiceInteractionService onReady()

前端之家收集整理的这篇文章主要介绍了Android API21:未调用VoiceInteractionService onReady()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对新的 Android API21有疑问,特别是VoiceInteractorService.

我想在API21中使用新的Hotword检测.如果我按下按钮,则应启动“google”一词的热门词检测.

当我有一个Activity(MainActivity)并且我想从VoiceInteractorService调用createAlwaysOnHotwordDetector(String keyphrase,Locale locale,AlwaysOnHotwordDetector.Callback callback)方法时,我收到一个错误:java.lang.IllegalStateException:在onReady()之前不可用调用.我尝试使用try catch循环来暂时解决这个问题,以查看何时调用onReady()并且我可以执行createAlwaysOnHotwordDetector()方法.我发现即使在让系统循环15分钟之后也永远不会调用onReady().

有人知道如何解决这个问题吗?

提前致谢.

这是我调用VoiceInteractorService的Activity.

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.service.voice.AlwaysOnHotwordDetector;
import android.service.voice.AlwaysOnHotwordDetector.Callback;
import android.service.voice.VoiceInteractionService;
import android.service.voice.AlwaysOnHotwordDetector.EventPayload;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btn;
    VoiceInteractionService service;
    AlwaysOnHotwordDetector.Callback callback;
    Locale locale = new Locale("nl-NL");
    Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        ctx = getApplicationContext();
        service = new VoiceInteractionService();
        btn.setOnClickListener(new OnClickListener() {
            // Create Hotword detector on button click
            @Override
            public void onClick(View v) {
                service.createAlwaysOnHotwordDetector("google",locale,callback);
            }
        });

        callback = new Callback() {

            @Override
            public void onRecognitionResumed() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onRecognitionPaused() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Log.d("error","error");
            }

            @Override
            public void onDetected(EventPayload eventPayload) {
                // TODO Auto-generated method stub
                // Display Toast message when Hotword is detected
                Toast.makeText(ctx,"Google",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onAvailabilityChanged(int status) {
                // TODO Auto-generated method stub
            }
        };
    }
}

解决方法

VoiceInteractionService旨在进行子类化以创建您自己的语音交互服务.一次只能启用一个这样的服务,就像键盘一样.一旦用户启用它,它就会在任何地方都处于活动状态,而不仅仅是在特定的活动中.

您无法使用VoiceInteractionService为特定活动提供语音识别.

原文链接:https://www.f2er.com/android/318547.html

猜你在找的Android相关文章