Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shara · Apr 22, 2011 at 12:41 AM · androidviewthreadactivity

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
               }
             });
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges