Using the SharedPreferences class you can create named maps of key/value pairs within your
application that can be shared among application components running in the same application
context.
call getSharedPreferences on the application Context
// Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = mySharedPreferences.edit();
// Store new primitive types in the shared preferences object.
editor.putBoolean("isTrue", true);
editor.putFloat("lastFloat", 1f);
editor.putInt("wholeNumber", 2);
editor.putLong("aNumber", 3l);
editor.putString("textEntryValue", "Not Empty");
// Commit the changes.
editor.commit();
RETRIEVING SHARED PREFERENCES
public static String MY_PREFS = "MY_PREFS";
public void loadPreferences() {
// Get the stored preferences
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode);
// Retrieve the saved values.
boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f);
int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1);
long aNumber = mySharedPreferences.getLong("aNumber", 0);
String stringPreference = mySharedPreferences.getString("textEntryValue", "");
}
SharedPreferences prefs;
Context context = getApplicationContext();
prefs = PreferenceManager.getDefaultSharedPreferences(context);
Defining a Preference Screen Layout in XML
preference definitions are stored in the res/xml resources folder
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
A simple Shared Preferences screen
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="My Preference Category"/>
<CheckBoxPreference
android:key="PREF_CHECK_BOX"
android:title="Check Box Preference"
android:summary="Check Box Preference Description"
android:defaultValue="true"
/>
</PreferenceCategory>
</PreferenceScreen>
Native Preference Controls
CheckBoxPreference
EditTextPreference
ListPreference
RingtonePreference
Using Intents to Import System Preference Screens
<PreferenceScreen
android:title="Intent preference"
android:summary="System preference imported using an intent">
<intent android:action="android.settings.DISPLAY_SETTINGS "/>
</PreferenceScreen>
Introducing the Preference Activity
public class MyPreferenceActivity extends PreferenceActivity {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
On Shared Preference Change Listener skeleton implementation
public class MyActivity extends Activity implements
OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle SavedInstanceState) {
// Register this OnSharedPreferenceChangeListener
Context context = getApplicationContext();
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// TODO Check the shared preference and key parameters and change UI or
// behavior as appropriate.
}
}
public class UserPreferences extends PreferenceActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
SAVING ACTIVITY STATE
protected void saveActivityPreferences(){
// Create or retrieve the activity preference object.
SharedPreferences activityPreferences = getPreferences(Activity.MODE_PRIVATE)
// Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = activityPreferences.edit();
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Store new primitive types in the shared preferences object.
editor.putString("currentTextValue", myTextView.getText().toString());
// Commit changes.
editor.commit();
}
Saving and Restoring Instance State
private static final String TEXTVIEW_STATE_KEY = "TEXTVIEW_STATE_KEY";
@Override
public void onSaveInstanceState(Bundle saveInstanceState) {
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Save its state
saveInstanceState.putString(TEXTVIEW_STATE_KEY, myTextView.getText().toString());
super.onSaveInstanceState(saveInstanceState);
}
SAVING AND LOADING FILES
String FILE_NAME = "tempfile.tmp";
// Create a new output file stream that’s private to this application.
FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
// Create a new file input stream.
FileInputStream fis = openFileInput(FILE_NAME);
String OUTPUT_FILE = "publicCopy.txt";
FileOutputStream fos = openFileOutput(OUTPUT_FILE, Context.MODE_WORLD_WRITEABLE);
INCLUDING STATIC FILES AS RESOURCES
Resources myResources = getResources();
InputStream myFile = myResources.openRawResource(R.raw.myfilename);
FILE MANAGEMENT TOOLS
deleteFile Enables you to remove files created by the current application.
fileList Returns a string array that includes all the files created by the current application