Android sharepreference Use

Source: Internet
Author: User

The Sharedpreferences class, which is a lightweight storage class, is especially suitable for saving software configuration parameters.

Sharedpreferences Save the data, behind it is the XML file to store data, files stored in the/data/data/<package name>/shared_prefs directory:

A simple storage code is as follows:

Sharedpreferences sharedpreferences = getsharedpreferences ("Wujay", context.mode_private); Private data

Editor editor = Sharedpreferences.edit ();//Get Editor

Editor.putstring ("name", "Wujaycode");

Editor.putint ("Age", 4);

Editor.commit ();//Submit Changes

The contents of the generated Wujay.xml file are as follows:

<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>

<map>

<string name= "Name" >wujaycode</string>

<int name= "Age" value= "4"/>

</map>

Analyze the following methods:

Yi, getsharedpreferences (name,mode)

The first parameter of the method specifies the name of the file, the name does not have a suffix, and the suffix is automatically added by Android;

The second parameter of the method specifies the mode of operation of the file, with a total of four modes of operation.

The four modes of operation are:

1. Mode_append: Append mode storage

2. Mode_private: Private mode storage, other apps cannot access

3. Mode_world_readable: Indicates that the current file can be read by another application

4. Mode_world_writeable: Indicates that the current file can be written by another application

Second, edit () method to get editor object

Editor editor = Sharedpreferences.edit ();

Editor storage objects are stored with Key-value key value pairs, editor.putstring ("name", "Wujaycode");

Submitting data through the commit () method

The corresponding method to obtain the data:

Sharedpreferences share=getsharedpreferences ("acitivity", activity.mode_world_readable);

int I=share.getint ("I", 0);

String str=share.getstring ("str", "");

Boolean Flag=share.getboolean ("flag", false);

GetString () The second parameter is the default value, and if the key does not exist in preference, the default value is returned

If you want to delete a file produced by sharedpreferences, you can do this by using the following methods:

File file= New file ("/data/data/" +getpackagename (). toString () + "/shared_prefs", "Activity.xml");

if (file.exists ()) {

File.delete ();

Toast.maketext (testactivity.this, "delete succeeded", Toast.length_long). Show (); }

Iii. access to preference in other applications

If you want to access preference in other apps, you must satisfy the condition that the preference of the app you want to access is created with context.mode_world_readable or context.mode_world_ Writeable permissions.

For example, if there is a <package name> for com.wujay.action The following application uses the following statement to create the preference,getsharedpreferences ("Wujay", Context.mode _world_readable),

Now to access the preferences:

First, you need to create the context above and then access preferences through the context, and the preference will be found in the Shared_prefs directory under your app's package when you access preference:

Context Otherappscontext = Createpackagecontext ("Com.wujay.action", context.context_ignore_security);

Sharedpreferences sharedpreferences = otherappscontext.getsharedpreferences ("Wujay", Context.MODE_WORLD_READABLE);

String name = sharedpreferences.getstring ("name", "");

int age = Sharedpreferences.getint ("Age", 0);

If you do not create a context to access another app's preference, you can directly access other applications preference the corresponding XML file in the form of a read XML file, such as:

File XMLFile = new file ("/data/data/<package name>/shared_prefs/itcast.xml");//<package name> should be replaced with the app's package name.

Turn from

http://m.blog.csdn.net/article/details?id=8501063

Android sharepreference Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.