- Home /
Executing function on main thread from Android Plugin
Hi,
I'm actually developping a mobile app, which uses vocal recognition. To do this, i made an Android Library and imported it. Unfortunately, i can't use my vocal recognition functions because i have an error saying to me that:
I/Unity: AndroidJavaException: java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread
I've tried a lot of things and here is what i did:
The script from where i launch my java object:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using UnityEngine.UI; using Newtonsoft.Json.Linq; using Vuforia;
public class TargetScript : MonoBehaviour, ITrackableEventHandler {
//Attributs du script
public AndroidJavaObject jo;
public AndroidJavaObject jo2;
public AndroidJavaObject voiceReco;
public AndroidJavaClass androidJC;
void Start()
{
init();
}
public void init()
{
this.androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
this.jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
this.voiceReco = new AndroidJavaObject("com.example.r_boug.myunitylib.VoiceRecognition", this.jo);
}
void Update()
{
}
Here is my Android Plugin Java class used: (VoiceRecognition):
package com.example.r_boug.myunitylib;
import android.content.Context;
import android.os.Handler;
import android.speech.SpeechRecognizer;
import android.util.Log;
import com.vuzix.speech.Constants;
import com.vuzix.speech.VoiceControl;
/**
* Created by r_boug on 02/05/2017.
*/
public class VoiceRecognition {
private VoiceControl vc;
public VoiceRecognition(Context context) {
// super(context);
// super(context);
Log.d("AFFICHAGE DU RESULTAT","FONCTION 1");
// Get a handler that can be used to post to the main thread
SpeechRecognizerThread speechRecognizer=new SpeechRecognizerThread(context);
speechRecognizer.run();
}
}
And finally here is the definition of the thread in the Android Plugin :
package com.example.r_boug.myunitylib;
import android.content.Context;
import android.util.Log;
import com.vuzix.speech.Constants;
import com.vuzix.speech.VoiceControl;
/**
* Created by r_boug on 02/05/2017.
*/
public class SpeechRecognizerThread implements Runnable {
public Context c;
public VoiceControl vc;
public SpeechRecognizerThread(Context parameter) {
// store parameter for later user
c=parameter;
}
public void run() {
vc=new VoiceControl(c) {
@Override
protected void onRecognition(String s) {
Log.d("RECONNAISSAINCE",s);
}
}; vc.addGrammar(Constants.GRAMMAR_NAVIGATION);
Log.d("AFFICHAGE DU RESULTAT","FONCTION 2");
vc.addGrammar(Constants.GRAMMAR_NAVIGATION);
Log.d("AFFICHAGE DU RESULTAT","FONCTION 3");
vc.on();
Log.d("AFFICHAGE DU RESULTAT","FONCTION 4");
}
}
As you can see, i've tried to call all the plugin functions in the context i passed from Unity. (Maybe isn't it the main application thread?). I know that you'll have ideas to help me !
Thanks,
Answer by liortal · May 07, 2017 at 05:25 AM
Check out this example of using AndroidJavaRunnable to achieve what you want: https://docs.unity3d.com/ScriptReference/AndroidJavaRunnable.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612121126im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bad performance on quad core Android phones 0 Answers
Unity Google+ plugin 0 Answers
Android Manifest Help 1 Answer
Using Ad plugins on Android leads to extra crashes 0 Answers