Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Java method from C++ / JNI which takes an Android Context parameter

I am trying to call into a Java class via C++/JNI on Android. More specifically, I am trying to call the constructor of this class which takes an Android Context as a parameter. I have no issue making the call successfully if my constructor has no params, but when I include the necessary Context as a parameter, I do not know what my JNI signature should look like and also doubt if that is even possible since I do not have access to that context object.

So, my question is, is it possible to call the constructor of a Java class which takes an Android Context as its only parameter ? If so, how ? If not, is there a workaround as I need the context to access certain Android API classes.

like image 220
Drake Amara Avatar asked Nov 04 '22 02:11

Drake Amara


1 Answers

I dont believe its possible in the manner you stated. However, w/o knowing the structure of your class, you could always create it as a singleton w/ a static instance which gets instantiated on start up of your Activity, thereby the class attains the required context at that time. It would essentially sit there until you're ready to call in from C++, but would indeed be available to service your request w/ the context.

like image 129
Bamerza Avatar answered Nov 08 '22 14:11

Bamerza