I. sharepreferences: Storing private raw data in a key-value pair
1. Two ways to get shareprefereces objects
(1) getsharedpreferences (String name, int mode)
(2) getpreferences (int mode)
2. Steps to write values
(1) Call Edit to get the Sharepreferences.editor object
(2) Adding a key-value pair using a method such as Putboolean (String key, Boolean value)
(3) Call commit () save
3. Read the value
By means of shareprefereces Getboolean (String key, Boolean defvalue) , etc.
eg
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 is from Android.context.Context sharedpreferences settings = getsharedpreferences (prefs_name, 0); C16/>sharedpreferences.editor Editor = Settings.edit (); Editor.putboolean ("Silentmode", Msilentmode); Commit the edits! Editor.commit (); }}
Two. Using internal memory
1. Create a file and write it to internal storage
(1) Call openfileoutput (String name, int mode), open a file (created without existing), return the FileOutputStream object
(2) Call write (byte[] buffer) to the file
(3) Call Close () to close the stream
String FILENAME = "Hello_file"; string string = "Hello world!"; FileOutputStream fos = openfileoutput (FILENAME, context.mode_private); Fos.write (String.getbytes ()); Fos.close ();
2. Read the file from the internal memory
(1) Call openfileinput (String name) to return the FileInputStream object
(2) Call Read (byte[] buffer, int byteoffset, int byteCount) reading stream
(3) Call Close () to close the stream
3. Some other useful methods
LOG.I (tag, Getcachedir (). GetPath ()) //data/data/package name/CACHESLOG.I (tag, Getfilesdir (). GetPath ()) //data/data /Package Name/files
Storage options (Storage options)