Android 中的数据存取方式之一: Preference(配置) .

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


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


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


数据写入:
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();


实例:

Manifest.xml

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

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PreferenceTestActivity"
            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>

</manifest>

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Preference Test" />

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:height="180px"
        android:text="" >
    </EditText>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" >
    </Button>

</LinearLayout>

PreferenceTestActivity.java
  1. package com.preference.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.SharedPreferences;  
  5. import android.os.Bundle;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8.   
  9. public class PreferenceTestActivity extends Activity {  
  10.     private EditText myEditText;  
  11.     private Button myBtn;  
  12.     public static final String SEND_SMS = "temp_sms";  
  13.       
  14.     /** Called when the activity is first created. */  
  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.         myBtn = (Button)findViewById(R.id.Button01);  
  22.           
  23.         SharedPreferences pre = getSharedPreferences(SEND_SMS, MODE_WORLD_READABLE);  
  24.         String content = pre.getString("sms_context""");  
  25.         myEditText.setText(content);  
  26.     }  
  27.   
  28.     @Override  
  29.     protected void onStop() {  
  30.         // TODO Auto-generated method stub   
  31.         super.onStop();  
  32.         SharedPreferences.Editor editor = getSharedPreferences(SEND_SMS, MODE_WORLD_READABLE).edit();  
  33.         editor.putString("sms_context", myEditText.getText().toString());  
  34.         editor.commit();  
  35.     }  
  36.       
  37.       
  38. }  
package com.preference.test;

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

public class PreferenceTestActivity extends Activity {
	private EditText myEditText;
	private Button myBtn;
	public static final String SEND_SMS = "temp_sms";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myEditText = (EditText)findViewById(R.id.EditText01);
        myBtn = (Button)findViewById(R.id.Button01);
        
        SharedPreferences pre = getSharedPreferences(SEND_SMS, MODE_WORLD_READABLE);
        String content = pre.getString("sms_context", "");
        myEditText.setText(content);
    }

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		SharedPreferences.Editor editor = getSharedPreferences(SEND_SMS, MODE_WORLD_READABLE).edit();
		editor.putString("sms_context", myEditText.getText().toString());
		editor.commit();
	}
    
    
}

在这个实例中,进到PreferenceTestActivity 后,有一个 EditText 可以编辑内容。在点HOME键该Activity 后,再进入该Activity,保存的内容不会丢失。而这些内容正好就保存在 preference 当中。
------------------------------------------------------------------------------------------------------------------------------------------------------
            if (!isSdcardExist()){
                Log.v(TAG, "on click1 set storage as phone sdcard...");
                SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFS_NAME, MODE_WORLD_READABLE).edit();  
                //editor.putString("preferences_record_mode", "1");
                editor.putString("preferences_storage_location", "0");
                //editor.putString("preferences_record_filetype", "1");
                editor.commit();
            }
------------------------------------------------------------------------------------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值