- Home /
Android -- accessing thread of the original Activity's views
Is there a way to access the original thread of the Activity's views for Android? I'm using an Android plugin that has a method which attaches a View (clickable image banner) onto the current Activity. This works fine when I have access to the main Activity thread, but how to implement this in Unity? In Android only the original thread which created the view can add views as subviews.
public class CallJavaCode : MonoBehaviour { public AndroidJavaClass bannerClass; public AndroidJavaObject bannerInstance;
void Start ()
{
// Attach our thread to the java vm; obviously the main thread is already attached but this is good practice..
JavaVM.AttachCurrentThread();
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
bannerClass = new AndroidJavaClass("com.package.bannerClass");
bannerClass.CallStatic("init", currentActivity);
bannerInstance = bannerClass.CallStatic<AndroidJavaObject>("getInstance");
}
void OnGUI ()
{
...
if (showBanner)
{
// Attach our thread to the java vm; obviously the main thread is already attached but this is good practice..
JavaVM.AttachCurrentThread();
bannerInstance.Call("showBannerAd", 15, 15);
showBanner = false;
}
}
}
// Android code
View banner;
Context appContext;
public void showBannerAd(int x, int y)
{
WindowManager mW = (WindowManager)appContext.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = mW.getDefaultDisplay().getWidth();
int screenHeight = mW.getDefaultDisplay().getHeight();
LinearLayout linearLayout = new LinearLayout(appContext);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, screenHeight));
linearLayout.setPadding(x, y, 0, 0);
linearLayout.addView(banner);
((Activity)appContext).addContentView(linearLayout, new ViewGroup.LayoutParams(screenWidth, screenHeight));
}
Howver this throws an exception because I'm trying to add a View to the main Activity while not in the main thread.
04-21 16:54:49.942: ERROR/AndroidRuntime(9233): FATAL EXCEPTION: GLThread 9 04-21 16:54:49.942: ERROR/AndroidRuntime(9233): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Is there a way to access the main Activity thread which created the views originally?
Answer by Suneet · Feb 28, 2013 at 03:12 AM
You can use UnityPlayer.currentActivity (which inherits Activity, so it has a runOnUiThread method, as follows:
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UnityPlayer.currentActivity, "This is a cool toast!", Toast.LENGTH_LONG).show();
}
});
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
null texture passed to GUI.DrawTexture 0 Answers
Extracting Dataset (AR) from OBB (Expansion Files) 1 Answer
GUIText Alpha transparency not working on Android Table? 0 Answers
Virtual reality enabled doesn't show stereoscopic view on Android 1 Answer