1

My requirement is to store data persistently in native android executable. The data is of primitive types. Can I use SharedPreferences in executables? If yes, then how ? and if NO, then is there any alternative for storing data persistently in native side android?

I don't want to use SQLite in native side or any file operation.

Thanking,

Sudhir Sinha
  • 693
  • 9
  • 18

2 Answers2

0

Edit: sorry I originally read the question wrong

Can I use SharedPreferences in executables?

Yes. If data is of primitive type you definitely should.

How?

From the android developers documentation:

public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){
   super.onCreate(state);
   . . .

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("silentMode", false);
   setSilent(silent);
}

@Override
protected void onStop(){
   super.onStop();

  // We need an Editor object to make preference changes.
  // All objects are from android.context.Context
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("silentMode", mSilentMode);

  // Commit the edits!
  editor.commit();
}

}

I posted a similar answer on how to use SharedPrefs here just take note that this use case of SharedPreferences breaks the benefits of the HashMap, which is really fast for finding and getting a value for a key.

One way you could do it is the following:

//if you are running the code inside from an Activity
Context context = this;
SharedPreferences userSharedPrefs = context.getSharedPreferences("USER_NAME_PREFS", MODE_PRIVATE);
SharedPreferences pwdSharedPrefs = context.getSharedPreferences("PWD_PREFS", MODE_PRIVATE);

The method getAll() will return a data structure called HashMap which works like a dictionary:

For each value stored there is a unique key.

sidenote: By getting them all at once you are kinda breaking the purpose of this data structure but let's continue

    Map<String, String> userNameHashMap = (Map<String, String>)userSharedPrefs.getAll();
    Map<String, String> pwdHashMap = (Map<String, String>)pwdSharedPrefs.getAll();

then you can do whatever you want with them

want them in a list? (I am assuming your user names are strings by the way)

    List<String> userNameList = new LinkedList<>();
    userNameList.addAll(userNameHashMap.values());

want to know if there's a password for user john?

    boolean johnHasPasswd = pwdHashMap.containsKey("john");
    String johnsPass;

    if(johnHasPasswd)
        johnsPass = pwdHashMap.get("john");

If you want to use the native data storage mechanisms, you are limited to the following

Your data storage options are the following:

  • Shared Preferences Store private primitive data in key-value pairs.
  • Internal Storage Store private data on the device memory.
  • External Storage Store public data on the shared external storage.
  • SQLite Databases Store structured data in a private database.
  • Network Connection Store data on the web with your own network server

You should have a look at these official docs from the developers website.

Hope this helps!

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • _"Based only on your requirements of not wanting SharedPreferences"_ The OP _does_ want to use `SharedPreferences` if possible. – Michael Oct 19 '16 at 12:31
  • 1
    Thanks for your response. I need to know how to use Shared Preferences Store private primitive data in key-value pairs in native C in android NDK. There is no JNI implementation in requirement since I am working on binaries/executable. – Sudhir Sinha Oct 19 '16 at 12:43
  • @SudhirSinha have you been able to solve your problem? – HenriqueMS Oct 24 '16 at 21:17
  • I don't have instance of JNIEnv since I am working on standalone binaries. For now to store data persistently in linux, I am using file and reading/writing file using mmap. This solved my problem and also file operation has been become speedy due to mmap. – Sudhir Sinha Oct 29 '16 at 05:05
  • Your answer seems to be from android sdk point of view which is correct but my question pertains to native side storage in android linux. – Sudhir Sinha Oct 29 '16 at 05:14
  • Going from JNI native side, it looks somewhat cumbersome to access sharedPreference and so currently storing data in some files, then fetching from that when required. In fact, sharedpreference is also implemented in the same manner. Thank you all for the inputs. – Sudhir Sinha Jul 18 '19 at 11:22
  • @Sudhir yes indeed, if you don't want to use the SDK, and assuming your linux user running the code has the correct permissions you should be able to do it – HenriqueMS Jul 18 '19 at 11:29
0

Shared preference are the best solution to store premitive data:

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,         Context.MODE_PRIVATE);
    Editor editor = sharedpreferences.edit();
    editor.putString("key", "value");
    editor.commit();
puneet yadav
  • 107
  • 4