I have a problem with the new Android API21, specifically the VoiceInteractorService.
I would like to use the new Hotword detection in API21. If i press a button, a hotword detection for the word 'google' should start.
When I have an Activity (MainActivity) and I want to call the createAlwaysOnHotwordDetector(String keyphrase, Locale locale, AlwaysOnHotwordDetector.Callback callback) method from the VoiceInteractorService, I get an error: java.lang.IllegalStateException: Not available until onReady() is called. I tried to solve this temporarily by using a while with try catch loop to see when onReady() is called and I can excecute the createAlwaysOnHotwordDetector() method. I found out that onReady() is never called, even after letting the system loop for 15 minutes.
Does anybody have an idea how to solve this problem?
Thanks in advance.
This is my Activity for calling the VoiceInteractorService.
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
}
};
}
}