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

Android数据存储(1)少量数据保存之SharedPreferences接口实例

2014-03-29 12:41 381 查看

SharedPreferences数据保存主要是通过键值的方式存储在xml文件中

xml文件在data/此程序的包名/XX.xml

格式

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

<map>

<int name="count" value="3" />

<string name="time">写入日期:2013年10月07日,时间:11:28:09</string>

</map>

SharedPreferences读写的基本步骤



1.通过Context的getSharedPreferences获取SharedPreferences接口的对象share:SharedPreferences share = this.getSharedPreferences("share",Context.MODE_PRIVATE);

"shre"保存的xml文件名 ,Context.MODE_PRIVATE 保存的类型为只被本程序访问 (还有MODE_WORLD_READABLE表示其余的程序能够读不能写,MODE

_WORLD_WRITEBLE能读写 这两个都在api17的时候被废了)

2.通过share的getXXX的方法获取指定key的值 : share.getInt("count", 0);



1.通过SharedPreferences对象的edit()方法获取Edit对象:Edit editor = share.edit();

2.通过editor对象的putXXX方法来写入值 :editor.putInt("count", 1);

3.调用Editor的commit()方法提交修改值 :editor.commit();

访问其他程序的SharedPreferences

访问其他程序的SharedPreferences 的读写唯一不同的是先的获取该程序的Context接口对象:this.createPackageContext(packageName, flags)

packageName为要该目标程序的包名,flags访问类型

其余的就和上面的步骤差不多 就不再概叙

实例

[html] view
plaincopy

<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:orientation="vertical"

tools:context=".MainActivity" >

<Button

android:id="@+id/write"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="写入数据" />

<Button

android:id="@+id/read"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="读入数据" />

<TextView

android:id="@+id/txtCount"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<TextView

android:id="@+id/txt1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

</LinearLayout>

[java] view
plaincopy

package com.android.xiong.sharepreferencestest;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

private Button write;

private Button read;

private TextView txt1;

private TextView countTxt;

SharedPreferences share ;

Editor editor;

int countO=0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获取SharedPreferences对象

share = this.getSharedPreferences("share",

Context.MODE_PRIVATE);

//获取Editor对象

editor = share.edit();

write = (Button) findViewById(R.id.write);

read = (Button) findViewById(R.id.read);

txt1 = (TextView) findViewById(R.id.txt1);

countTxt=(TextView)findViewById(R.id.txtCount);

//获取share中key为count的值

countO=share.getInt("count", 0);

countO++;

//修改share中key为count的值

editor.putInt("count", countO);

//提交修改

editor.commit();

System.out.println("该应用程序使用了:"+countO+"次");

countTxt.setText("该应用程序使用了:"+countO+"次");

OnClickListener writeListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

SimpleDateFormat data = new SimpleDateFormat(

"写入日期:yyyy年MM月dd日,时间:hh:mm:ss");

editor.putString("time",

data.format(new Date()));

editor.commit();

}

};

OnClickListener readListener=new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(!share.contains("share")){

txt1.setText(share.getString("time", null));

}

}

};

write.setOnClickListener(writeListener);

read.setOnClickListener(readListener);

}

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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐