This example describes how Android stores data without using a database. Share to everyone for your reference. The specific analysis is as follows:
In some cases we do not need to build a database, but we want to save some data, wait until the next time the program is run, then how do we do?
1. Referencing namespaces
Import android.content.SharedPreferences;
2. Define a new class Pictureglobaldef to store the data, defined in the 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 you want to refer to the data switch_open:
Sharedpreferences Settingviewmode = getsharedpreferences (
picturenoteglobaldef.appsetting, 0);
Boolean bswitch = Settingviewmode.getboolean (
picturenoteglobaldef.default_switch_mode_key,
Picturenoteglobaldef.switch_open);
4. Where you want to save the data switch_open:
Picturenoteglobaldef.switch_open = Bswitch;
Sharedpreferences Settingviewmode = getsharedpreferences (appsetting,0);
Sharedpreferences.editor Editor = Settingviewmode.edit ();
Editor.putboolean (Picturenoteglobaldef.default_switch_mode_key,
picturenoteglobaldef.switch_open);
Editor.commit ();
5. Read and write the sharedpreferences of other applications
Sometimes we need to read and write the sharedpreferences of other applications, what should we do then?
The key to reading sharedpreferences other applications is to get the context of other applications:
Context tempcontext = null;
Tempcontext = Createpackagecontext ("a.b", context.context_ignore_security);
Here A.B represents the package name for the application
This gets the context of the other application.
After getting to the context, you can use the Getsharedpreferences method of the context to get the Shaerdpreferences object, which reads and writes data according to the 1-4 method.
I hope this article will help you with your Android program.