码迷,mamicode.com
首页 > 移动开发 > 详细

Android 使用SharedPreferences进行数据存储和读取数据

时间:2014-09-30 23:56:30      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:android   sharedpreferences   

很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件或者xml进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下。

下面用默认路径的设置,举例说明其用法,首先是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/hello"
	    />
    <EditText
    	android:layout_width="fill_parent" 
	    android:layout_height="wrap_content"
	    android:id="@+id/pathName"
	    />
	<Button
		android:layout_width="wrap_content" 
	    android:layout_height="wrap_content"
	    android:text="@string/button"
	   	android:id="@+id/btn_savePath"
	    />

	<TextView
	    android:id="@+id/explaition"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/pathset_explation" />

</LinearLayout>


下面是PreferencesService类:

package lhjd.cnc.dispenser.setting;

import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class PreferencesService
{
	private Context context;
	public PreferencesService(Context context)
	{
		this.context = context;
	}
	/**
	 * 实现应用参数保存
	 * @param name
	 * @param age
	 * @throws Exception
	 */
	public void save(String name) throws Exception
	{
		SharedPreferences preferences=context.getSharedPreferences("pathSet", Context.MODE_WORLD_READABLE);
		Editor editor=preferences.edit();
		editor.putString("name", name);
		editor.commit();
	}
	
	/**
	 * 实现应用参数提取
	 * @return
	 * @throws Exception
	 */
	public Map<String, String> getPreferences() throws Exception
	{
		SharedPreferences preferences=context.getSharedPreferences("pathSet", Context.MODE_WORLD_READABLE);
		Map<String, String> result=new HashMap<String, String>();
		result.put("name", preferences.getString("name", ""));
		return result;
	}
}

下面是activity

public class PathSet extends Activity{
	
	private EditText pathNameText;
	private PreferencesService service;
	public PathSet(final Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.pathset, this);
		service=new PreferencesService(context);
		pathNameText=(EditText)this.findViewById(R.id.pathName);
	    Map<String, String> params = null;
		try {
			params = service.getPreferences();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pathNameText.setText(params.get("name"));
        Button button=(Button)findViewById(R.id.btn_savePath);
        button.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				String name=pathNameText.getText().toString().trim();
				if("".equals(name)||name==null){
					Toast.makeText(context, "路径不能为空", 1).show();
				}else{
					try
					{
						service.save(name);
						Toast.makeText(context, R.string.succss, 1).show();
					} catch (Exception e)
					{
						Toast.makeText(context, R.string.fail, 1).show();
						e.printStackTrace();
					}
				}
			
			}
		});
    }
}


下面是截图效果:

bubuko.com,布布扣

Android 使用SharedPreferences进行数据存储和读取数据

标签:android   sharedpreferences   

原文地址:http://blog.csdn.net/gisredevelopment/article/details/39700551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!