且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-03 10:21:24

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

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

数据读取

 


  1. 1.  
  2. String PREFS_NAME = "Note.sample.roiding.com";     
  3.  
  4. 2.  
  5. SharedPreferences settings = getSharedPreferences(PREFS_NAME,  0);     
  6.  
  7. 3.  
  8.  boolean silent = settings.getBoolean("silentMode", false);     
  9.  
  10. 4.  
  11. 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

 


  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.SharedPreferences;  
  7. import android.os.Bundle;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.  
  11. public class MainActivity extends Activity {  
  12.     private EditText myEditText;  
  13.     private Button b1;  
  14.     private static final String TEMP_SMS="temp_sms";  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.           
  20.         myEditText = (EditText)findViewById(R.id.EditText01);  
  21.         b1 = (Button)findViewById(R.id.Button01);  
  22.           
  23.         SharedPreferences pre = getSharedPreferences(TEMP_SMS, MODE_WORLD_READABLE);  
  24.         String content = pre.getString("sms_content", "");  
  25.         myEditText.setText(content);  
  26.           
  27.     }  
  28.       
  29.     @Override  
  30.     protected void onStop() {  
  31.         super.onStop();  
  32.         SharedPreferences.Editor editor = getSharedPreferences(TEMP_SMS, MODE_WORLD_WRITEABLE).edit();  
  33.         editor.putString("sms_content", myEditText.getText().toString());  
  34.         editor.commit();  
  35.     }  

/Chapter09_Data_01/res/layout/main.xml

 


  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9. <TextView    
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content" android:text="Preference Test"/> 
  12. <EditText   
  13. android:text=""   
  14. android:id="@+id/EditText01"   
  15. android:layout_width="fill_parent"   
  16. android:layout_height="wrap_content" 
  17. android:height="180px" 
  18. ></EditText> 
  19. <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"></Button> 
  20. </LinearLayout> 

/Chapter09_Data_01/AndroidManifest.xml

 


  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  5.       package="com.amaker.test" 
  6.       android:versionCode="1" 
  7.       android:versionName="1.0"> 
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".MainActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.  
  17.     </application> 
  18.     <uses-sdk android:minSdkVersion="3" /> 
  19.  
  20. </manifest> 

 


本文转自linzheng 51CTO博客,原文链接:

http://blog.51cto.com/linzheng/1079368