David Given David Given - 1 month ago 10
Android Question

Getting a Context from a NativeActivity

I have an application using NativeActivity. I want to call out into Java to do something that requires a Context (e.g., accessing TelephonyManager to query the IMEI).

How do I get a valid Context for my activity?

The ANativeActivity structure contains a variety of useful data for my application, such as the JavaVM, and JNI environment, etc. It also contains a jobject pointing at my NativeActivity's class object. What it doesn't contain is a jobject pointing at the NativeActivity's instance. I don't see anything useful in

struct android_app
, either.

It must be possible to do this; any ideas how?

Answer

Despite its name the clazz member of ANativeActivity struct is actually pointing to android.app.NativeActivity instance.
Check this post for an example of using a JNI call from the Native Activity to Java code.