Android SharedPreferences存储

原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SharedPreferences.html                      

一 概念

  SharedPreferences存储方式是Android中存储轻量级数据的一种方式。SharedPreferences存储主要用来存储一些简单的配置信息,内部以Map方式进行存储,因此需要使用键值对提交和保存数据,保存的数据以xml格式存放在本地的/data/data/<package name>/shares_prefs文件夹下。

 

二 特点

1,        使用简单,便于存储轻量级的数据;

2,        只支持Java基本数据类型,不支持自定义数据类型;

3,        SharedPreferences是单例对象,在整个应用内数据共享,无法在其他应用内共享数据;

 

三 注意事项

1,        SharedPreferences创建的时候使用的文件名不同,得到的对象不同,在存储位置会创建多个xml文件,不同文件名的SharedPreferences的数据不会共享;创建时采用相同的文件名,得到多个SharedPreferences引用,此时这多个引用共享同一个xml文件,它们操作的数据为相同的数据;

2,        针对数据的增删改查操作只有在提交后操作才能生效,此步骤最容易被忽略,当执行了对数据的操作后得不到需要的效果时,请查看是否没有提交操作。

3,        三种创建模式中最好采用MODE_PRIVATE,使用其他两种模式创建容易引起安全问题,就算采用MODE_PRIVATE,当别人活得root权限后也可能泄露用户的重要信息,因此建议使用SharedPreferences时,如果要存储用户名和密码时,不要明文存储,应该使用加密存储,防止重要隐私泄露,引起损失。

 

四 使用

1,        获得SharedPreferences对象

SharedPreferences对象必须使用上下文获得,使用时注意先要获得上下文。获得SharedPreferences对象方法为:

SharedPreferences sharedPreferences = getSharedPreferences(参数一, 参数二);

参数一为要保存的xml文件名,不同的文件名产生的对象不同,但同一文件名可以产生多个引用,从而可以保证数据共享。此处注意指定参数一时,不用加xml后缀,由系统自动添加。

参数二为创建模式,共有三个值:

MODE_PRIVATE

MODE_WORLD_READABLE

MODE_WORLD_WRITEABLE 

第一个值使得SharedPreferences存储的数据只能在本应用内获得,第二个和第三个值分别使得其他应用可以读和读写本应用SharedPreferences存储的数据。由此可能带来安全问题,请参考本文二(3)部分。

2,        获得editor对象

使用以上获得的SharedPreferences对象产生editor,方法为:

Editor editor = sharedPreferences.edit();

3,        对数据实现增删改查

添加数据使用以下方法:

editor.putString(key, value);

可以实现数据的更新只需添加同键的键值对,和操作Map集合一样。

删除数据:

editor.remove(key);

删除参数部分键的键值对。

查询数据:

String result = sharedPreferences.getString(key1, key2);

Key1是要查询的键,返回对应的值,当键不存在时,返回key2作为结果

清空数据

editor.clear();

 

注意:无论是否同一个sharedPreferences对象,若是产生多个editor,不同的editor之间对数据的操作不会相互影响,此处容易犯错误,例如,以下的程序:

sharedPreferences.edit().putString(key1, value1);

sharedPreferences.edit().putString(key2, value2);

sharedPreferences.edit().putString(key3, value3);

sharedPreferences.edit().commit();

执行后这种方式无法存储,因为sp.edit()每次都会返回一个新的Editor对象,Editor的实现类EditorImpl里面会有一个缓存的Map,最后commit的时候先将缓存里面的Map写入内存中的Map,然后将内存中的Map写进XML文件中。使用上面的方式commit,由于sp.edit()又重新返回了一个新的Editor对象,缓存中的Map是空的,所以导致数据无法被存储。这里即需要注意,只有提交时,不同的editor无法完成既定的任务。而增加,删除,更新,查询,若是同一个sharedPreferences产生的editor,则对共享的数据有效,会按照既定的顺序工作。而不同sharedPreferences产生的editor则因为处理的xml文件不同而不能共享数据。

再次强调:所有使用editor操作的数据,必须经过同一个editor提交即commit后才能生效。

 

Android实例

以下使用Android实例完成以上内容的验证:

1,        Activity界面和功能

 

(以下描述控件的顺序是布局中的由上到下)

保存数据功能:在第一个EditText中写入要保存的键,在第二个EditText中写入要保存对应键的值,点击第一个按钮则保存数据;

查询数据功能:在第三个EditText中写入要查询的键,点击第二个按钮查询数据,查询出的值显示在第一个TextView中;

删除数据功能:在第四个EditText中写入要删除的键,点击第三个按钮则删除键和值;

清空数据功能:点击第四个按钮则清空与editor对应的xml文件中的所有数据,但不会影响其他xml文件。

 2,        主要代码

①MainActivity.java

package com.example.sharedpreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private EditText et_key;
    private EditText et_value;
    private EditText et_query;
    private SharedPreferences sharedPreferences;
    private TextView tv_query;
    private EditText et_delete;
    private Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sharedPreferences = getSharedPreferences("info2", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        
        et_key = (EditText) findViewById(R.id.et_key);
        et_value = (EditText) findViewById(R.id.et_value);
        et_query = (EditText) findViewById(R.id.et_query);
        tv_query = (TextView) findViewById(R.id.tv_content);
        et_delete = (EditText) findViewById(R.id.et_delete);
    }
    
    public void insert(View v) {
        //获取键值数据
        String key = et_key.getText().toString().trim();
        String value = et_value.getText().toString().trim();        
        
        //使用editor保存数据
        editor.putString(key, value);
        
        //注意一定要提交数据,此步骤容易忘记
        editor.commit();
        
    }
    public void query(View v) {
        
        //获得查询的键
        String query_text = et_query.getText().toString().trim();
        
        //使用SharedPreferences查询数据
        String result = sharedPreferences.getString(query_text, null);
        
        if(result == null) {
            tv_query.setText("您查询的数据不存在");
        }else {
            tv_query.setText(result);
        }
    }
    
    public void delete(View v) {
        
        //获得删除的键
        String delete = et_delete.getText().toString().trim();
        //使用editor删除数据
        editor.remove(delete);
        //一定要提交,该步骤非常容易忽略
        editor.commit();
    }
    
    public void clear(View v) {
        
        //使用editor清空数据
        editor.clear();
        //一定要提交,该步骤非常容易忽略
        editor.commit();
    }
    
}

 ②activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >    

    <EditText
        android:id="@+id/et_key"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_key" />
    
    <EditText
        android:id="@+id/et_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_value" />  
       
   
    <Button
       android:id="@+id/bt_insert"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="right"       
       android:onClick="insert"
       android:text="@string/text_insert" /> 
       
    <EditText
       android:id="@+id/et_query"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="@string/text_et_value" />  
  
    <Button
       android:id="@+id/bt_query"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="right"
       android:onClick="query"
       android:layout_marginLeft="88dp"
       android:text="@string/text_bt_query" />
    
    <TextView 
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  
        android:hint="@string/text_tv_query"  
        android:textSize="20sp"    
        />
      
    <Button 
        android:id="@+id/bt_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_bt_delete"
        android:layout_gravity="right"
        android:onClick="delete"
        />
        
    <EditText
        android:id="@+id/et_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_et_delete" />      
    
    <Button 
        android:id="@+id/bt_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_bt_clear"
        android:layout_gravity="right"
        android:onClick="clear"
        />
   
</LinearLayout>

 

posted @ 2016-11-12 23:13  chironmy  阅读(12638)  评论(0编辑  收藏  举报