标签:
说明:
1. SharedPreferences是使用键值对的方式来存储数据的.
2. SharedPreferences保存的数据将持续整个用户会话,即使你的应用程序被杀掉(killed),直到应用程序卸载
3. SharedPreferences有三种模式:
4. Android提供了三种方式得到SharedPreferences 对象:本质上都是调用Context 类中的getSharedPreferences()方法:
使用:
1. 向SharedPreferences中写入数据,分为四步:
2. 向SharedPreferences中读取数据,分为两步:
结构:
代码:
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.linux.sharedpreferencestest"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"> 12 <intent-filter> 13 <action android:name="android.intent.action.MAIN" /> 14 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 </application> 19 20 </manifest>
MainActivity.java:
1 package com.example.linux.sharedpreferencestest; 2 3 import android.content.Context; 4 import android.content.SharedPreferences; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.View; 8 import android.widget.TextView; 9 import android.widget.Toast; 10 11 import java.util.HashSet; 12 import java.util.Set; 13 14 /** 15 * writer: 胡红翔 16 * date: 2016-03-10 17 * function: sharedPreference的简单使用 18 */ 19 public class MainActivity extends AppCompatActivity { 20 private TextView textView; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 textView = (TextView) findViewById(R.id.textView); 27 } 28 29 // 在share preference中写入数据 30 public void write(View view) { 31 //PreferenceManager.getDefaultSharedPreferences(this); 32 SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE); 33 //SharedPreferences preferences = this.getSharedPreferences("Linux", Context.MODE_PRIVATE); 34 SharedPreferences.Editor editor = preferences.edit(); 35 36 Set<String> stringSet = new HashSet<>(); 37 stringSet.add("Huhx"); 38 stringSet.add("Tomhu"); 39 editor.putInt("int", 3); 40 editor.putString("name", "Linux"); 41 editor.putStringSet("set", stringSet); 42 43 editor.commit(); 44 Toast.makeText(MainActivity.this, "write to sharde preference success!", Toast.LENGTH_SHORT).show(); 45 } 46 47 //在share preference中读取数据 48 public void read(View view) { 49 SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE); 50 int intData = preferences.getInt("int", 0); 51 String name = preferences.getString("name", "无名"); 52 Set<String> set = preferences.getStringSet("set", null); 53 54 StringBuffer stringBuffer = new StringBuffer(); 55 stringBuffer.append("int: " + intData + "\n"); 56 stringBuffer.append("name: " + name + "\n"); 57 int length = set == null ? 0 : set.size(); 58 stringBuffer.append("set size: " + length + "\n"); 59 60 textView.setText(stringBuffer.toString()); 61 Toast.makeText(MainActivity.this, "read from shared preference success!", Toast.LENGTH_SHORT).show(); 62 } 63 }
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.example.linux.sharedpreferencestest.MainActivity"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Hello World!" /> 14 15 <Button 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:onClick="write" 19 android:text="writeToShare" /> 20 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:onClick="read" 25 android:text="readFromShare" /> 26 </LinearLayout>
原理:
1 public final class EditorImpl implements Editor { 2 private final Map<String, Object> mModified = Maps.newHashMap(); 3 private boolean mClear = false; 4 5 public Editor putString(String key, @Nullable String value) { 6 synchronized (this) { 7 mModified.put(key, value); 8 return this; 9 } 10 } 11 }
1 @Nullable 2 public String getString(String key, @Nullable String defValue) { 3 synchronized (this) { 4 awaitLoadedLocked(); 5 String v = (String)mMap.get(key); 6 return v != null ? v : defValue; 7 } 8 }
1 SharedPreferencesImpl(File file, int mode) { 2 mFile = file; 3 mBackupFile = makeBackupFile(file); // 生成bak备份文件 4 mMode = mode; 5 mLoaded = false; 6 mMap = null; 7 startLoadFromDisk(); //命名文件,解析xml文件,并且将键值对存放到Map中 8 }
链接:
android基础---->SharedPreferences的使用
标签:
原文地址:http://www.cnblogs.com/huhx/p/5240091.html