You want an easy way to store simple information, such as strings and primitive values.You can use SharedPreferences to easily store and retrieve data.
SharedPreferences prefs = getSharedPreferences("myPrefs", ➥ Context.MODE_PRIVATE); Editor editor = prefs.edit(); editor.putString("HW_KEY", "Hello World"); editor.commit(); //. . . later, or from another component String helloWorld = prefs.getString("HW_KEY", "default value");
You need to allow users to set preferences for your application and easily persist them to SharedPreferences files.
first, we want to include a preference screen that allows us to enable or disable the splash screen.
Our Preferences Activity for MyMoviesDatabase, which will be accessible via the menu once we’re done, is seen in figure 7.6.
There’s a preference resource XML file that defines the elements, and there’s a PreferenceActivity.
modify the androidmanifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.mymoviesdatabases" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SplashScreen" android:label="@string/title_activity_main" android:theme="@style/SplashScreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyMovies" /> <activity android:name=".MovieSearch" /> <activity android:name=".MovieDetail"/> <activity android:name=".CategoryManager" /> <activity android:name=".Preferences" /> </application> </manifest>
add the style:
<style name="SplashScreen" parent="@android:style/Theme.Black"> <item name="android:windowNoTitle">true</item> </style>
<?xml version="1.0" encoding="UTF-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Application Settings"> <CheckBoxPreference android:title="Splash Screen" android:key="showsplash" android:summary="Disabled" android:defaultValue="false" /> </PreferenceCategory> </PreferenceScreen>
public class Preferences extends PreferenceActivity { /* * PreferenceActivity shows a hierarchical list of Preference objects tothe user, * and automatically saves the selections to a backing SharedPreferences file. */ private CheckBoxPreference showSplash; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.addPreferencesFromResource(R.layout.preferences);//set the XML preference hierarchy in the activity // handle to preferences doesn't come from findViewById! //obtain a reference to a specific preference object that’s declared in the XML showSplash=(CheckBoxPreference)this.getPreferenceScreen().findPreference("showsplash"); this.setCheckBoxSummary(showSplash); // listen to see if user changes pref, so we can update display of current value SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this); prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener(){ @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // TODO Auto-generated method stub if(key.equals("showsplash")){ setCheckBoxSummary(showSplash); } } }); } private void setCheckBoxSummary(CheckBoxPreference pref) { if (pref.isChecked()) { pref.setSummary("Enables"); } else { pref.setSummary("Disabled"); } } }