Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3609354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:37:01+00:00 2026-05-18T21:37:01+00:00

Is there any way in which I can pass/get an object of android context

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:37:02+00:00Added an answer on May 18, 2026 at 9:37 pm

    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:

    //Somewhere inside a function in your native code
    void Java_com_example_native_MainActivity_nativeFunction(JNIEnv* env, jobject thiz)
    {
        jclass cls = (*env)->FindClass(env,"PreferenceHelper");
        if (cls == 0) printf("Sorry, I can't find the class");
    
        jmethodID set_preference_method_id;
    
        if(cls != NULL)
        {
            set_preference_method_id = (*env)->GetStaticMethodID(env, cls, "setPreference", "(Ljava/lang/String;Ljava/lang/StringV");
    
            if(set_preference_method_id != NULL )
            {
                jstring preference_name = (*env)->NewStringUTF(env, "some_preference_name");
                jstring value = (*env)->NewStringUTF(env, "value_for_preference");
    
                (*env)->CallStaticVoidMethod(env, cls, get_main_id, preference_name, value);
            }
        }
    }
    

    Note that i just wrote the code from memory so expect not to work out of the box.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way in which I can automatically convert a Custom Class Object
I want to know if there is any way by which I can paste
Is there any way in JSF which can be used to find out whether
Is there any way by which I can increase the default width of the
Is there any way by which i can extract a private key from a
Is there any way by which we can export the result of a select
is there any way via which we can interact to iphone scheduler and insert
Is there any way by using JDT ASTParser, by which we can identify if
Is there any way that I can list the pages which are currently stored
Is there any way to identify which SQL Agent job is running a stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.