您的位置:首页 > 移动开发 > Android开发

Android采用SharedPreferences方式进行文件的保存与读取

2015-01-28 15:00 423 查看

工程目录:



做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等耗时大大影响了程序的效率,因此我们使用键值这种一一对应的关系来存放这些配置信息。SharedPreferences正是Android中用于实现这中存储方式的技术。

下面直接贴实现代码:
package peixun.savaparameter.service;

PreferenceService类:



<span style="font-size:18px;">package peixun.savaparameter.service;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.Preference;

public class PreferenceService {
	private Context	context;

	public PreferenceService(Context context) {
		super();
		this.context = context;
	}

	public void save(String name, Integer age) {
		// TODO Auto-generated method stub
		SharedPreferences preference=context.getSharedPreferences(name, Context.MODE_PRIVATE);
		Editor editor=preference.edit();
		editor.putString("name", name);
		editor.putInt("age", age);
		editor.commit();
	}
	
	public Map<String,String> getParameter(String name){
		
		Map<String, String> params=new HashMap<String, String>();
		SharedPreferences preference=context.getSharedPreferences(name, Context.MODE_PRIVATE);
		params.put("name", preference.getString("name", ""));
		params.put("age", String.valueOf(preference.getInt("age", 0)));
		
		return params;	
	}

}</span><strong>
</strong>
注解:
//打开Preferences,名称为name,如果存在则打开它,否则创建新的Preferences,写入方式为Context.MODE_PRIVATE。

SharedPreferences preference=context.getSharedPreferences(name, Context.MODE_PRIVATE);

Editor editor=preference.edit();//让Editor处于编辑状态

package peixun.savaparameter;

MainActivity类:

package peixun.savaparameter;

import java.util.Map;

import peixun.savaparameter.service.PreferenceService;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText nameText;
	private EditText ageTest;
	private PreferenceService service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		nameText=(EditText) this.findViewById(R.id.name);
		ageTest=(EditText) this.findViewById(R.id.age);
		service=new PreferenceService(this);
		String name=nameText.getText().toString();
		Map<String, String> paramers=service.getParameter(name);
		nameText.setText(paramers.get("name"));
		ageTest.setText(paramers.get("age"));
	}
	public void save(View v)
	{
		String name=nameText.getText().toString();
		String age=ageTest.getText().toString();
		PreferenceService service=new PreferenceService(getApplicationContext());
		service.save(name,Integer.valueOf(age));
		Toast.makeText(getApplicationContext(), R.string.savaSuccess, 1).show();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


layout目录下的界面activity_main.xml文件:

<?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/name"
    />
	<EditText 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:id="@+id/name"/>
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="@string/age"/>
	<EditText
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:id="@+id/age"/>
	<Button 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="@string/savaButton"
	    android:onClick="save"
	    android:id="@+id/saveButton"/>
    
</LinearLayout>


values目录下的String.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">保存参数</string>
    <string name="action_settings">Settings</string>
    <string name="name">姓名</string>
    <string name="age">年龄</string>
    <string name="savaButton">保存参数</string>
    <string name="savaSuccess">保存成功</string>
    <string name="savaFailed">保存失败</string>
    

</resources>


运行结果截图:



在File Explorer下的data目录下的data文件下有一个shared_prefs文件,保存的文件路劲就这这里面,文件名为我们输入的姓名。

SharedPreferences的使用非常简单,能够轻松的存放数据和读取数据。SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐