android中的数据存取-方式一:preference(配置)

     这种方式应该是用起来最简单的Android读写外部数据的方法了。他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,那么这些设置最后就可以 通过Preferences来保存,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。当然,如果你愿意保存其他的东西,也没有什么限制。只是在性能上不知道会有什么问题。

在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME/shared_prefs 目录下。

数据读取

  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" ); 
String PREFS_NAME = "Note.sample.roiding.com";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

boolean silent = settings.getBoolean("silentMode", false);

String hello = settings.getString("hello", "Hi");

这段代码中:
  • SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    通过名称,得到一个SharedPreferences,顾名思义,这个Preferences是共享的,共享的范围据现在同一个Package中,这里面说所的Package和Java里面的那个Package不同,貌似这里面的Package是指在AndroidManifest.xml文件中:

     

    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" >  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    
    package="com.roiding.sample.note"
    
    android:versionCode="1"
    
    android:versionName="1.0.0">
    
    

    这里面的package。根据我目前的实验结果看,是这样的,欢迎指正。后面的那个int是用来声明读写模式,先不管那么多了,暂时就知道设为0(android.content.Context.MODE_PRIVATE)就可以了。

  • boolean silent = settings.getBoolean(”silentMode”, false);
    获得一个boolean值,这里就会看到用Preferences的好处了:可以提供一个缺省值。也就是说如果Preference中不存在这个值的话,那么就用后面的值作为返回指,这样就省去了我们的if什么什么为空的判断。

数据写入

  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(); 
String PREFS_NAME = "Note.sample.roiding.com";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

SharedPreferences.Editor editor = settings.edit();

editor.putBoolean("silentMode", true);

editor.putString("hello", "Hello~");

editor.commit();

有了上面数据读取的代码,这里面的就容易理解了,只是别忘了最后的commit();

 

 

实例:

/Chapter09_Data_01/src/com/amaker/test/MainActivity.java

android中的数据存取-方式一:preference(配置) 代码
    
      
package com.amaker.test;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
private EditText myEditText;
private Button b1;
private static final String TEMP_SMS = " temp_sms " ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);

myEditText
= (EditText)findViewById(R.id.EditText01);
b1
= (Button)findViewById(R.id.Button01);

SharedPreferences pre
= getSharedPreferences(TEMP_SMS, MODE_WORLD_READABLE);
String content
= pre.getString( " sms_content " , "" );
myEditText.setText(content);

}

@Override
protected void onStop() {
super .onStop();
SharedPreferences.Editor editor
= getSharedPreferences(TEMP_SMS, MODE_WORLD_WRITEABLE).edit();
editor.putString(
" sms_content " , myEditText.getText().toString());
editor.commit();
}
}

/Chapter09_Data_01/res/layout/main.xml

android中的数据存取-方式一:preference(配置) 代码
    
      
<? 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"
>
< TextView
android:layout_width ="fill_parent"
android:layout_height
="wrap_content" android:text ="Preference Test" />
< EditText
android:text =""
android:id
="@+id/EditText01"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:height
="180px"
></ EditText >
< Button android:id ="@+id/Button01" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text ="Send" ></ Button >
</ LinearLayout >

/Chapter09_Data_01/AndroidManifest.xml

android中的数据存取-方式一:preference(配置) 代码
    
      
<? xml version="1.0" encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.amaker.test"
android:versionCode
="1"
android:versionName
="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".MainActivity"
android:label
="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >

</ application >
< uses-sdk android:minSdkVersion ="3" />

</ manifest >

 

 

 

 

 

你可能感兴趣的:(Preference)