Many times we develop software to provide users with software parameter settings, such as our commonly used QQ, users can set whether to allow strangers to add their own friends. For the preservation of software configuration parameters, if it is window software usually we will use INI file to save, if it is J2SE application, we will use Properties property file or XML to save. If it's Android apps, what's the best way to save software configuration parameters? The Android platform provides us with a sharedpreferences class, a lightweight storage class that is especially good for saving software configuration parameters. Use Sharedpreferences to save data, behind which you store data in an XML file, and the file is stored in the/data/data/<package name>/shared_prefs directory:
Sharedpreferences sharedpreferences = getsharedpreferences ("LJQ", context.mode_private);
Editor Editor = Sharedpreferences.edit ();//Get Editor
Editor.putstring ("name", "Lin Yi-chin");
Editor.putint ("Age", 24);
Editor.commit ()//Commit modification
The resulting Ljq.xml file contains the following:
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map>
<string name= "name" > Lin Yi-chin </string>
<int name= "age" value= "/>"
</map>
Because the sharedpreferences behind is the use of XML files to save data, thefirst parameter of the Getsharedpreferences (Name,mode) method is used to specify the name of the file, with no suffix for the name. The suffix will be automatically added by Android. The second parameter of the method specifies the mode of operation for the file, which has four modes of operation, which are explained earlier in this article when saving data using file format. If you want the XML file used behind Sharedpreferences to be read and written by other applications, you can specify context.mode_world_readable and context.mode_world_writeable permissions.
In addition, the activity also provides another getpreferences (mode) method operation Sharedpreferences, which defaults to the name of the file with the class masterpiece with the current class without the package name.
accessing Data in Sharedpreferences
The data code in the Access Sharedpreferences is as follows:
Sharedpreferences sharedpreferences = getsharedpreferences ("LJQ", context.mode_private);
GetString () The second parameter is the default value, and if the key does not exist in preference, the default value is returned
String name = sharedpreferences.getstring ("name", "");
int age = Sharedpreferences.getint ("Age", 1);
If you access preference in another application, the prerequisite is that the preference specified context.mode_world_readable or context.mode_world_writeable permissions when it was created.
For example: There is a <package name> for com.ljq.action application that uses the following statement to create the preference.
Getsharedpreferences ("Ljq", context.mode_world_readable);
Other applications to access the above application of the preference, first need to create the context of the above application, and then access preference through the context, access to preference will be under the application package Shared_ Prefs directory to find preference:
Context Otherappscontext = Createpackagecontext ("Com.ljq.action", context.context_ignore_security);
Sharedpreferences sharedpreferences = otherappscontext.getsharedpreferences ("LJQ", context.mode_world_readable);
String name = sharedpreferences.getstring ("name", "");
int age = Sharedpreferences.getint ("Age", 0);
If you do not access the preference of other applications by creating a context, you can also access other application preference corresponding XML files in the form of reading XML files, such as:
File XmlFile = new file ("/data/data/<package name>/shared_prefs/itcast.xml");//<package name> should be replaced with the package name of the application
Case:
String.xml file
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello World, spactivity!</string>
<string name= "App_name" > Software configuration Parameters </string>
<string name= "name" > Name </string>
<string name= "Age" > Ages </string>
<string name= "button" > Save Settings </string>
<string name= "Showbutton" > Show </string>
</resources>
Main.xml Layout file
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<textview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/name"
Android:textsize= "20px"
Android:id= "@+id/namelable"/>
<edittext android:layout_width= "80px"
android:layout_height= "Wrap_content"
android:layout_torightof= "@id/namelable"
android:layout_aligntop= "@id/namelable"
android:layout_marginleft= "10px"
Android:id= "@+id/name"/>
</RelativeLayout>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<textview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "20px"
android:text= "@string/age"
Android:id= "@+id/agelable"/>
<edittext android:layout_width= "80px"
android:layout_height= "Wrap_content"
android:layout_torightof= "@id/agelable"
android:layout_aligntop= "@id/agelable"
android:layout_marginleft= "10px"
Android:id= "@+id/age"/>
</RelativeLayout>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<button android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/button"
Android:id= "@+id/button"/>
<button android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/showbutton"
android:layout_torightof= "@id/button"
android:layout_aligntop= "@id/button"
Android:id= "@+id/showbutton"/>
</RelativeLayout>
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:textsize= "20px"
Android:id= "@+id/showtext"/>
</LinearLayout>
Copy Code code as follows:
Package com.ljq.activity;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Spactivity extends activity {
Private EditText Nametext;
Private EditText Agetext;
Private TextView Resulttext;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Nametext = (edittext) This.findviewbyid (r.id.name);
Agetext = (edittext) This.findviewbyid (r.id.age);
Resulttext = (TextView) This.findviewbyid (R.id.showtext);
Button button = (button) This.findviewbyid (R.id.button);
Button Showbutton = (button) This.findviewbyid (R.id.showbutton);
Button.setonclicklistener (listener);
Showbutton.setonclicklistener (listener);
Echo
Sharedpreferences sharedpreferences=getsharedpreferences ("ljq123"),
context.mode_world_readable+context.mode_world_writeable);
String namevalue = sharedpreferences.getstring ("name", "");
int agevalue = Sharedpreferences.getint ("Age", 1);
Nametext.settext (Namevalue);
Agetext.settext (string.valueof (Agevalue));
}
Private View.onclicklistener listener = new View.onclicklistener () {
public void OnClick (View v) {
Button button = (button) v;
ljq123 files are stored in the/data/data/<package name>/shared_prefs directory
Sharedpreferences sharedpreferences=getsharedpreferences ("ljq123"),
context.mode_world_readable+context.mode_world_writeable);
Switch (Button.getid ()) {
Case R.id.button:
String name = Nametext.gettext (). toString ();
int age = Integer.parseint (Agetext.gettext (). toString ());
Editor Editor = Sharedpreferences.edit (); Get editor
Editor.putstring ("name", name);
Editor.putint (' age ', age);
Editor.commit ()//Commit modification
Toast.maketext (Spactivity.this, "Save Success", Toast.length_long). Show ();
Break
Case R.id.showbutton:
String namevalue = sharedpreferences.getstring ("name", "");
int agevalue = Sharedpreferences.getint ("Age", 1);
Resulttext.settext ("Name:" + Namevalue + ", Age:" + agevalue);
Break
}
}
};
}
Run results
How do I access the preference in other applications?
Copy Code code as follows:
Package com.ljq.sp;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.test.AndroidTestCase;
Import Android.util.Log;
public class Accesssharepreferencetest extends androidtestcase{
private static final String TAG = "Accesssharepreferencetest";
/**
* Access to sharepreference One, note: permissions should be sufficient
* @throws Exception
*/
public void Testaccesspreference () throws exception{
String Path = "/data/data/com.ljq.activity/shared_prefs/ljq123.xml";
File File = new file (path);
FileInputStream InputStream = new FileInputStream (file);
Gets an XML string
String data = new Fileservice (). read (InputStream);
LOG.I (TAG, data);
}
/**
* Access to Sharepreference Two, note: permission is sufficient
* @throws Exception
*/
public void TestAccessPreference2 () throws exception{
Context context = This.getcontext (). Createpackagecontext ("Com.ljq.activity",
context.context_ignore_security);
Sharedpreferences sharedpreferences = context.getsharedpreferences ("ljq123",
context.mode_world_readable+context.mode_world_writeable);
String name = sharedpreferences.getstring ("name", "");
int age = Sharedpreferences.getint ("Age", 1);
LOG.I (TAG, name + ":" +age);
}
}