- Home /
How to create Android Handler in Java plugin
So, let's say you have a Java plugin, you call into the Java plugin like so
private static readonly AndroidJavaClass m_somePlugin = new AndroidJavaClass("com.unity3d.Plugin.blah.SomePlugin");
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
m_somePlugin.CallStatic("onInitialise", currentActivity);
}
}
and it looks something like this
public class SomePlugin
{
static public void onInitialise(final Activity currentActivity)
{
Handler someHandler = new Handler();
}
}
All quite simple. Except it will crash. Creating a Handler is the cause. I'm guessing it's a thread issue.
So the question is, how does one create a handler in a Java plugin? Anyone know?
looks like currentActivity.runOnUiThread may be the way to go. I'll try it and report on here if it is successful.
Answer by HazeTI · Nov 12, 2012 at 04:25 PM
Yes, the solution to the problem was to use runOnUiThread. So in order to get the above code to not crash SomePlugin should look like so
public class SomePlugin
{
static public void onInitialise(final Activity currentActivity)
{
currentActivity.runOnUiThread(new Runnable() {
Handler someHandler = new Handler(); });
}
}
Your answer

Follow this Question
Related Questions
Problem in android java plugin 0 Answers
How do you access a custom Java class from Unity? 1 Answer
Calling static jar function from Unity3D 0 Answers
Send message to Java Code (without a plugin)? 1 Answer
Compass java plugin for Unity Android 4 Answers