Is there any way in which I can pass/get an object of android context in my ndk appliation. I want to use SharedPreferences in my ndk application via jni interface. To get an instance of SharedPreferences object, I need to call getSharedPreferences() on Context object. But I do not have access to the context object.
OR
How can I read and write an xml file from NDK ?
Any pointers will be appreciated.
There is nothing special you have to do, it is just like regular JNI mechanism. You need to get a pointer to the context object, then retrieve the method ID you want to call and then invoke it with the args you want.
Of course in words it sounds super straightforward, but in code it gets really messy since the all the checks and JNI calls.
So in my opinion i will not try to implement the whole thing from native/JNI code, instead i will implement a helper method in Java that makes all the stuff and just receives the needed data to read/write the preference.
That will simplify a lot your native code and it will make it easier to maintain.
eg:
Note that i just wrote the code from memory so expect not to work out of the box.