Read / Write Configuration Information using Preferences

Learn:

  • Preferences

Requirements

  • Android smartphone or emulator

Android provides several options for you to save persistent application data. SharedPreferences provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Run the preference_write.py to save the value and subsequent running of preference_read.py will retrieve the stored value.

preference_write.py

import android droid = android.Android() putstoreval = droid.dialogGetInput('Store Value').result print putstoreval droid.prefPutValue("mystoreval",putstoreval,"mypref.txt")

putstoreval is assigned to the key mystoreval and stored in the preferences file mypref.txt

preference_read.py

import android droid = android.Android() getstoreval = droid.prefGetValue("mystoreval","mypref.txt").result print getstoreval

More information about the preferences api

Scan the script to your smartphone using the Test EMANT380 app or download to PC/Emulator

preferences.zip