我对新的
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 } }; } }