Data access in android-Method 1: preference (configuration) and androidpreference

Source: Internet
Author: User

Data access in android-Method 1: preference (configuration) and androidpreference

This method should be the simplest method for Android to read and write external data. Its usage is basically the same as J2SE (java. util. prefs. preferences) in the same usage, in a simple and transparent way to save some user personalized settings of the font, color, location and other parameter information. Generally, applications provide interfaces such as "Settings" or "Preferences". These settings can be saved through Preferences, programmers do not need to know in what form it is stored and where it is saved. Of course, if you want to save other things, there is no limit. I just don't know what the problem will be.

In the Android system, the information is saved in the/data/PACKAGE_NAME/shared_prefs directory as an XML file.

Data Reading

1 String PREFS_NAME = "Note.sample.roiding.com";2 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);3 boolean silent = settings.getBoolean("silentMode", false);4 String hello = settings.getString("hello", "Hi");
In this Code:
  • SharedPreferences settings = getSharedPreferences (PREFS_NAME, 0 );
    Get a SharedPreferences by name. As the name suggests, this Preferences is shared. The shared range is different from the Package in Java, it seems that the Package here refers to the Package in AndroidManifest. xml file:
    1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"2 package="com.roiding.sample.note"3 android:versionCode="1"4 android:versionName="1.0.0">

    Package. According to my current experiment results, this is the case. please correct me. The int next to it is used to declare the read/write mode. No matter how much it is, set it to 0 (android. content. Context. MODE_PRIVATE.

  • Boolean silent = settings. getBoolean ("silentMode", false );
    Get a boolean value. Here we will see the benefit of using Preferences: You can provide a default value. That is to say, if this value does not exist in Preference, the following value is used as the return indicator, thus eliminating the need to determine if or empty.

Data Writing

1 String PREFS_NAME = "Note.sample.roiding.com";2 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);3 SharedPreferences.Editor editor = settings.edit();4 editor.putBoolean("silentMode", true);5 editor.putString("hello", "Hello~");6 editor.commit();

With the code for reading the above data, it is easy to understand here, but don't forget the final commit ();

 

Instance:

For details, see reprint: http://www.cnblogs.com/linzheng/archive/2011/01/22/1942073.html

Related Article

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.