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 VamshiKrishnaP · Feb 01, 2014 at 09:12 AM · androidnative

How to call a Android native class with implements from unity ?

Hi ,

How to call a native android method from Unity ?

native method :

public void sharePhoto(final String textPath, final String imagePath) {

 }




From Unity am calling like :

 testClass.Call("sharePhoto","hai Sir","http://stereo.gsfc.nasa.gov/beacon/latest_256/ahead_euvi_195_latest.jpg");


Result :

am not getting any error. but unable to enter into the method.

How to call ?

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 gfoot · Feb 04, 2014 at 09:03 PM

I think the problem is that testClass is an AndroidJavaClass - that is, it refers to the class itself, not to an instance of the class. That makes it good for calling static methods, but not non-static ones.

You need to create an instance instead, using AndroidJavaObject and issue the Call method on that, using the same syntax you're using now.

 AndroidJavaObject testObject = new AndroidJavaObject(className, constructorArg1, constructorArg2, ...);
 testObject.Call(methodName, methodArg1, methodArg2, ...);

The first line constructs the object by calling an appropriate constructor, dependent on the arguments you pass. The second line calls a method on the object.

Comment
Add comment · Show 5 · 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
avatar image VamshiKrishnaP · Feb 05, 2014 at 04:47 AM 0
Share

yes @gfoot,

I have tried the same as your comment

Unity Code :

   AndroidJavaClass  _$$anonymous$$yActivityClass = new AndroidJavaClass("com.unity3d.player.TestProjectActivity");     
   AndroidJavaObject  _ActivityObject = _$$anonymous$$yActivityClass.GetStatic<AndroidJavaObject>("currentActivity");   
     
     _ActivityObject.Call("sharePhoto","hai Sir","http://stereo.gsfc.nasa.gov/beacon/latest_256/ahead_euvi_195_latest.jpg");




am getting an error like : "NoSuchFieldError : no static field with name='CurrentActivity' .......

( refer attached Image)![alt text][1]

How to create an instance ?

screen shot 2014-02-03 at 7.09.08 pm.png (83.1 kB)
avatar image gfoot · Feb 05, 2014 at 09:47 AM 0
Share

See this question regarding accessing currentActivity.

The main point is that you have to request the currentActivity from com.unity3d.player.UnityPlayer, not from your own class. The object you get back is still going to be of your derived type (assu$$anonymous$$g you set the manifest up correctly) and so you can call your sharePhoto method directly on that.

avatar image VamshiKrishnaP · Feb 05, 2014 at 10:41 AM 0
Share

yes gfoot ... i also tried with com.unity3d.player.UnityPlayer , geting an error "NoSuchFieldError : no static field with name='CurrentActivity' .......

avatar image toritobravom · Mar 25, 2014 at 03:48 PM 0
Share

Did you get the solution?

avatar image gfoot · Mar 26, 2014 at 09:35 AM 0
Share

@toritobravom, this question diverged from the original topic - it started being about calling methods, then turned into getting the current activity reference. It may be better to ask a separate one.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

native android plugin 0 Answers

Android - Trying to Load Library bug 0 Answers

Call native android constructor 2 Answers

A node in a childnode? 1 Answer

Unity + Android + Development Build = CompareBaseObjectsInternal can only be called from the main thread. 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