牛骨文教育服务平台(让学习变的简单)

1,使用SharedPrefrences

用于简单少量的数据,数据的格式简单:都是普通的字符串,标量类型的值等,比如各种配置信息等等

SharedPrefrences与Editor简介

创建SharedPreferences实例,通过Context.getSharedPreferences(String name,int mode);方法来获取SharedPreferences的实例
mode的值:
*Context.MODE_PRIVATE;该SharedPreferences数据只能被本应用程序调读,写

  • Context.MODE_WORLD_READABLE;该SharedPreferences数据能被其他程序读,但是不能写
  • Context.MODE_WORLD_WRITEABLE;该SharedPreferences数据能被其他程序读,写

 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此他保存的数据主要是简单类型的key-value对

  • SharedPreferences接口主要负责读取应用程序的Preferences数据,提供如下常用的方法访问key-value对
  • boolean contains(String key);判断是否包含key的数据
  • abstract Map getAll();获取全部键值对
  • boolean getXxx(String key,xxx,defValue);获取指定的key对应的value值,如果key不存在,返回默认defvalue,xxx可以是Boolean,float,int,long,String等各种基本类型的值

SharedPreferences接口本身并没有提供写入数据的能力,而是通过 SharedPreferences的内部接口Editor写入数据,SharedPreferences调用edit()方法即可获得它所对应的Editor对象
Editor提供了如下方法:

  • SharedPreferences.Editor clear();清空所有数据
  • SharedPreferences.Editor putXxx(String key,xxx value);存入指定key对应的数据,xxx可以是Boolean,float,int,long,String等各种基本类型的值
  • SharedPreferences.Editor remove(String key);删除指定key的数据
  • Boolean commit();当Editor编辑完成之后,调用该方法提交修改

例子:一个按钮写数据,一个按钮读数据

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Write_SharedPreference" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Read_SharedPreference" />

</LinearLayout>



MainActivity.java


package com.hust.sharedpreferences;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/*
 * 创建SharedPreferences实例,通过Context.getSharedPreferences(String name,int mode);方法来获取SharedPreferences的实例
 * mode的值:
 * Context.MODE_PRIVATE;该SharedPreferences数据只能被本应用程序调读,写
 * Context.MODE_WORLD_READABLE;该SharedPreferences数据能被其他程序读,但是不能写
 * Context.MODE_WORLD_WRITEABLE;该SharedPreferences数据能被其他程序读,写
 * 
 * 
 * SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此他保存的数据主要是简单类型的key-value对
 * 
 * SharedPreferences接口主要负责读取应用程序的Preferences数据,提供如下常用的方法访问key-value对
 *    boolean contains(String key);判断是否包含key的数据
 *    abstract Map<String,?> getAll();获取全部键值对
 *    boolean getXxx(String key,xxx,defValue);获取指定的key对应的value值,如果key不存在,返回默认defvalue,xxx可以是Boolean,float,int,long,String等各种基本类型的值
 *    
 * SharedPreferences接口本身并没有提供写入数据的能力,而是通过   SharedPreferences的内部接口Editor写入数据,SharedPreferences调用edit()方法即可互殴它所对应的Editor对象
 * Editor提供了如下方法:
 *   SharedPreferences.Editor clear();清空所有数据
 *   SharedPreferences.Editor putXxx(String key,xxx value);存入指定key对应的数据,xxx可以是Boolean,float,int,long,String等各种基本类型的值
 *   SharedPreferences.Editor remove(String key);删除指定key的数据
 *   Boolean commit();当Editor编辑完成之后,调用该方法提交修改
 *   
 * */

public class MainActivity extends Activity {
	//
	SharedPreferences preferences;
	SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化SharedPreferences对象,读数据
        preferences=getSharedPreferences("test",Context.MODE_WORLD_READABLE);
        //实例化Editor对象,写数据
        editor=preferences.edit();
        
        Button read=(Button) findViewById(R.id.button2);
        Button write=(Button) findViewById(R.id.button1);
        read.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String time=preferences.getString("time", null);				
				int rnd=preferences.getInt("rnd", 0);
				String result=time==null?"您暂时还未写入数据":"写入时间:"+time+"
上次生成的数据数是:"+rnd;
				Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
			}
        	
        });
        write.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
				editor.putString("time", sdf.format(new Date()));
				editor.putInt("rnd", (int)(Math.random()*1000));
				editor.commit();

			}
        	
        });
    }

    @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);
    }
}


SharedPrefrences文件的存储位置:


test.xml

待续。。。