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] viewplaincopy
<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;
}
}
相关文章推荐
- Android数据存储(1)少量数据保存之SharedPreferences接口实例
- Android数据存储(1)少量数据保存之SharedPreferences接口实例
- Android少量数据保存之SharedPreferences接口实例
- Android的SharedPreferences保存与删除数据简单实例
- Android系统的五种数据存储形式实例(一)
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android——配置文件的保存SharedPreferences进行数据存储
- Android 数据存储——SQLite实例、判断数据库中表是否存在
- Android-文件存储-将数据保存在data目录下
- 实例详解Android文件存储数据方式
- android创建数据库(SQLite)保存图片示例ZZ 分类: Android数据存储 2015-03-22 09:58 41人阅读 评论(0) 收藏
- (九)android 中数据存储与访问——保存文件到手机内存
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android数据存储(2)向机身内存(Internal Storage)和SDcard(External Storage)读写数据实例
- Android 文件数据存储实例详解
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android——配置文件的保存SharedPreferences进行数据存储
- Android 保存图片到SQLite,读出SQLite中的图片 分类: Android数据存储 2015-03-22 09:59 39人阅读 评论(0) 收藏
- Android 将少量的数据文件保存在 data/data 目录下