SharedPreferences主要是存储一些简单的基本数据类型在xml文件中,并且采用内容观察者模式来监听数据变化,从而进行相应的操作。
(1)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/myButton" android:layout_below="@id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请点击我" /> </RelativeLayout>
(2)MySharedPreferences.java
package com.example.testsharedpreferencedemo; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class MySharedPreferences { public static final String MySharedPreferencesName= "shared_preferences_data"; private Context context = null; private SharedPreferences sp = null; private Editor editor = null; public static final String Key_Int_Count = "Key_Int_Count"; public static final int Value_Int_Count = 0; public MySharedPreferences(Context context){ this.context = context; this.sp = this.context.getSharedPreferences(MySharedPreferencesName,0); this.editor = sp.edit(); } public int getIntCount() { return sp.getInt(Key_Int_Count, Value_Int_Count); } public void setIntCount(int intCount) { editor.putInt(Key_Int_Count, intCount); editor.commit(); } public SharedPreferences getSharedPrefere(){ return sp; } }
(3)MainActivity.java
package com.example.testsharedpreferencedemo; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; public class MainActivity extends Activity implements OnClickListener, OnSharedPreferenceChangeListener{ public static final String TAG = "testsharedpreferencedemo"; private TextView myTextView = null; private Button myButton = null; private MySharedPreferences mySharedPre = null; public static final String clicl_count = "您点击了Button的次数为:"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { // TODO Auto-generated method stub mySharedPre = new MySharedPreferences(this); mySharedPre.setIntCount(0); mySharedPre.getSharedPrefere().registerOnSharedPreferenceChangeListener(this); myTextView = (TextView) findViewById(R.id.myTextView); myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(this); myTextView.setText(clicl_count + mySharedPre.getIntCount()); } @Override public void onClick(View view) { // TODO Auto-generated method stub if(view == myButton){ setXMLData(); } } private void setXMLData() { // TODO Auto-generated method stub mySharedPre.setIntCount(mySharedPre.getIntCount()+1); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPre, String key) { // TODO Auto-generated method stub Log.i(TAG, "onSharedPreferenceChanged"); if(key.equals(MySharedPreferences.Key_Int_Count)){ updateUI(); } } private void updateUI() { // TODO Auto-generated method stub myTextView.setText(clicl_count + mySharedPre.getIntCount()); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mySharedPre.getSharedPrefere().unregisterOnSharedPreferenceChangeListener(this); } }
http://download.csdn.net/detail/hfreeman2008/7808941