This article describes the method of Android storing data without using a database. Share it for your reference. The specific analysis is as follows:
In some cases, we do not need to build a database, but we need to save some data and wait until the program is called when it is run next time. So how do we do it?
1. Reference namespace
import ;
2. Define a new class PictureGlobalDef to store data, and define it in this class:
public final static String APPSetting = "SettingFile"; public final static String DEFAULT_SWITCH_MODE_KEY="default_switch_mode"; public static boolean SWITCH_OPEN = false;
3. Where to reference the data SWITCH_OPEN:
SharedPreferences settingviewMode = getSharedPreferences( , 0); boolean bSwitch = ( PictureNoteGlobalDef.DEFAULT_SWITCH_MODE_KEY , PictureNoteGlobalDef.SWITCH_OPEN );
4. Where to save the data SWITCH_OPEN:
PictureNoteGlobalDef.SWITCH_OPEN = bSwitch; SharedPreferences settingviewMode = getSharedPreferences(APPSetting,0); editor = (); (PictureNoteGlobalDef.DEFAULT_SWITCH_MODE_KEY , PictureNoteGlobalDef.SWITCH_OPEN ); ();
5. Read and write SharedPreferences for other applications
Sometimes, we need to read and write SharedPreferences of other applications. What should we do at this time?
The key to reading SharedPreferences for other applications is to get Context for other applications:
Context tempContext = null; tempContext = createPackageContext("",Context.CONTEXT_IGNORE_SECURITY); //Here is the package name of the application
This gets the Context of other applications
After obtaining the Context, you can use the getSharedPreferences method of the Context to obtain the ShaerdPreferences object, so that the data can be read and written according to the 1-4 method.
I hope this article will be helpful to everyone's Android programming design.