SharedPreferences是Android平台上一個輕量級的儲存類,主要是儲存一些常用的配置比如視窗狀態,一般在Activity中 重載視窗狀態onSaveInstanceState儲存一般使用SharedPreferences完成,它提供了Android平台常規的Long長 整形、Int整形、String字串型的儲存,它是什麼樣的處理方式呢?SharedPreferences類似過去Windows系統上的ini設定檔,但是它分為多種許可權,可以全域共用訪問,android123提示最 終是以xml方式來儲存,整體效率來看不是特別的高,對於常規的輕量級而言比SQLite要好不少,如果真的儲存量不大可以考慮自己定義檔案格式。xml
處理時Dalvik會通過內建底層的本地XML Parser解析,比如XMLpull方式,這樣對於記憶體資源佔用比較好。
這種方式應該是用起來最簡單的Android讀寫外部資料的方法了。他的用法基本上和 J2SE(java.util.prefs.Preferences)中的用法一樣,以一種簡單、 透明的方式來儲存一些使用者個人化的字型、顏色、位置等參數資訊。一般的應用程式都會提供“設定”或者“喜好設定”的這樣的介面,那麼這些設定最後就可以 通過Preferences來儲存,而程式員不需要知道它到底以什麼形式儲存的,儲存在了什麼地方。當然,如果你願意儲存其他的東西,也沒有什麼限制。只 是在效能上不知道會有什麼問題。
在Android系統中,這些資訊以XML檔案的形式儲存在 /data/data/PACKAGE_NAME /shared_prefs 目錄下。
下面是程式碼:
package com.cloay;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.EditText;import android.widget.ImageButton;/** * * MySharedPreferencesActivity.java * @author cloay * 2011-10-18 */public class MySharedPreferencesActivity extends Activity {private EditText user = null;private EditText password = null;private ImageButton loginBtn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); user = (EditText)findViewById(R.id.user); password = (EditText)findViewById(R.id.pass); loginBtn = (ImageButton)findViewById(R.id.loginButton); initView(); loginBtn.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction()==MotionEvent.ACTION_DOWN){v.setBackgroundResource(R.drawable.dengluxitong1);SharedPreferences userInfo = getSharedPreferences("user_info", 0);userInfo.edit().putString("name", user.getText().toString()).commit();userInfo.edit().putString("pass", password.getText().toString()).commit();}else if(event.getAction()==MotionEvent.ACTION_UP){v.setBackgroundResource(R.drawable.dengluxitong);}return false;} }); }private void initView() {SharedPreferences userInfo = getSharedPreferences("user_info", 0);String username = userInfo.getString("name", "");String pass = userInfo.getString("pass", "");user.setText(username);password.setText(pass);}}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
通過名稱,得到一個SharedPreferences,顧名思義,這個Preferences是共用的,共用的範圍據現在同一個Package中,這裡 面說所的Package和Java裡面的那個Package不同,貌似這裡面的Package是指在AndroidManifest.xml檔案中的
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cloay" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MySharedPreferencesActivity" 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>
布局檔案如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="185dp" android:id="@+id/user"
android:layout_height="40dp" android:hint="請輸入使用者名稱"
android:singleLine="true" android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/pass" android:layout_marginTop="66dp">
<requestFocus></requestFocus>
</EditText>
<EditText android:inputType="textPassword"
android:layout_width="185dp" android:id="@+id/pass"
android:layout_height="40dp" android:hint="請輸入密碼" android:singleLine="true"
android:layout_below="@+id/user" android:layout_centerHorizontal="true"
android:layout_marginTop="44dp">
</EditText>
<ImageButton android:layout_height="40dp"
android:layout_width="80dp" android:id="@+id/loginButton"
android:background="@drawable/dengluxitong"
android:layout_centerVertical="true" android:layout_alignRight="@+id/pass"
android:layout_marginRight="17dp"></ImageButton>
</RelativeLayout>
運行結果如下,首次顯示的時空白,第二次運行時如下: