- Home /
Calling a java method from a second thread is not working
I have this little function calling a java (android) method:
public static void Compose(string initialText, string url, string image, Action<bool> callback) {
Loom.RunAsync(() => {
AndroidJNI.AttachCurrentThread();
bool rs;
using (AndroidJavaClass jc = new AndroidJavaClass("com.beavl.android.ShareTools")) {
rs = jc.CallStatic<bool>("share", initialText, url, image);
}
Loom.QueueOnMainThread(() => {callback(rs);});
});
}
Note: Loom is a multithreading library that worked perfectly so far for me. It's here.
And the function just doesn't work, it does nothing as if it was never called.
If I remove the Loom parts of the function, so I call it from the main thread, it does work.
I have a log on the first line of the function to be sure when it's called or not.
Any idea why i'm getting this behaviour?
It looks like C# but you mention Java and the main thread so I reckon you are in the wrong place. This is for Unity that does not use Java. You better go to StackOverflow which takes all kind of issues (including Unity). I close the answer but if for some reasons I would be wrong I will open it again.
Answer by whydoidoit · Aug 22, 2013 at 06:31 AM
What is the return value of AndroidJNI.AttachCurrentThread();? Can you Debug.Log it?
@whydoidoit: AndroidJNI.AttachCurrentThread() result is 0. As a note, I Debug.Log it before calling the java function and in the logcat nothing is printed after that. @fafase: I'm calling an android java function from C#. @Bunny83: I'm using adb logcat!
Your answer
Follow this Question
Related Questions
Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer
How use the Java Native Interface in Unity outside of Android? 1 Answer
AndroidJava and Implementing java.util.List 0 Answers
BroadcastReceiver onReceive not called from custom Unity android plugin 1 Answer