1.SharedPreferences (Key-value)
The data that Sharedpreferences saves is primarily data that resembles the configuration information format, so the saved data is primarily a simple type of key-value pair (Key-value), which holds an XML file.
(1) two ways to create Sharedpreferences: getsharedpreferences (String name, int mode)----If you need to pass an identity (name) To differentiate between the different sharedpreferences files
getpreferences (int mode)----If you only need to use one sharedpreferences in an activity, you don't need to distinguish between
Mode: is the specified read and write mode, the value of three kinds, respectively:
context.mode_private: Specifies that the sharedpreferences data can only be read and written by this application
context.mode_world_readable: Specifies that the sharedpreferences data can be read by other applications but cannot be written
context.mode_world_writeable: Specifies that the sharedpreferences data can be read and written by other applications.
(2) Edit sharedpreferences
Method name |
Describe |
Public abstract Boolean contains (String key) |
Determine if Sharedpreferences contains data for a specific key |
Public abstract Sharedpreferences.editor Edit () |
Returns an edit object for Operation Sharedpreferences |
Public abstract map<string,?> getAll () |
Get all the Key-value in the sharedpreferences data. |
GetXXX (String key,xxx Defvlaue) |
Gets the value of the sharedpreferences data specified by key, and returns the default value of Defvalue if the key does not exist. where xxx can be a Boolean, float, int, long, string, and other basic types of values |
(3) Write data and read data
Method name |
Describe |
Public abstract Sharedpreferences.editor Clear () |
Empty all the data in the sharedpreferences. |
Public abstract Boolean commit () |
When editor is finished, call this method to commit the modification, and you must call this data to modify |
Public abstract Sharedpreferences.editor putxxx (String key, Typexxx XXX) |
The data corresponding to the specified key is deposited to the sharedpreferences, where xxx can be a Boolean, float, int, long, string, and other basic types of values |
Public abstract Sharedpreferences.editor GetXXX (String key, typexxx XXX) |
Take out the data corresponding to the specified key to the sharedpreferences, where xxx can be a Boolean, float, int, long, string, and other basic types of values |
Public abstract Sharedpreferences.editor Remove (String key) |
Delete the data item corresponding to the specified key in the Sharedpreferences |
2.Saving files
All of Android?? The OID device has two file stores: "Internal" and "external" storage. External storage is mainly SD card;
Internal storage area: Always available, and the access of their own applications;
External storage device: not always available, by the user or some software hang after it is not available, this is world readable, so the file saved here may be outside the control read. External storage is the best place to have access to restricted files and to share with other applications or allow users to access files with a computer;
(1) Set your app to get External storage permissions (read and write)
<manifest ...> <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> ... </manifest><manifest ...> <uses-permission android:name= "android.permission.READ_EXTERNAL_ STORAGE "/> ...</manifest>
You do not need to set any permissions to store files in an internal storage area
(2) Save the file in an internal storage area
There are two ways to get an internal storage file directory:
Getfilesdir (): Returns the directory file that represents the inside of your application.
Getcachedir (): Returns files in the internal directory representing the temporary cache files for your application
To create a non-cached directory file method:
File File = new file (Context.getfilesdir (), filename); String filename = "myfile"; String string = "Hello world!"; FileOutputStream outputstream;try { outputstream = openfileoutput (filename, context.mode_private); Outputstream.write (String.getbytes ()); Outputstream.close ();} catch (Exception e) { e.printstacktrace ();}
Create a cache directory under File method:
Public file GetTempFile (context context, String URL) { file file; try { String fileName = uri.parse (URL). getlastpathsegment (); File = File.createtempfile (FileName, NULL, Context.getcachedir ()); catch (IOException e) { //Error while creating file } return file;}
(3) Save the file in an external storage area
Because the external storage device may not be able to obtain the situation such as not or is mounted, so before use must be detected if available, using method getexternalstoragestate () If the resulting value equalsMEDIA_MOUNTED,则表示外部存储设备是可使用的;
/* Checks If external storage is available for read and write */public Boolean isexternalstoragewritable () { String St ate = Environment.getexternalstoragestate (); if (Environment.MEDIA_MOUNTED.equals (state)) { return true; } return false;} /* Checks If external storage is available to at least read */public Boolean isexternalstoragereadable () { String stat E = Environment.getexternalstoragestate (); if (Environment.MEDIA_MOUNTED.equals (state) | | Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) { return true; } return false;}
Android about saving data (saving)