Android少量数据保存之SharedPreferences接口实例
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访问类型
其余的就和上面的步骤差不多 就不再概叙
实例
<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>
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;
} }
最新文章
- 总结js常用函数和常用技巧(持续更新)
- 【C# 小窍门】WeakEventManager 无法识别!ErrorCS0246The type or namespace name &#39;WeakEventManager&#39; could not be found
- Provisioning Profile文件在哪找?
- asp.net Routing 用法
- 使用Lucene.NET实现简单的站内搜索
- linux iostat命令详解 磁盘操作监控工具
- HtmlAgilityPack相关网页
- 如何定制Sink扩展.Net Remoting功能
- Java 使用反射拷贝对象一般字段值
- mongo设计(三)
- 大约sql声明优化
- sql server 2012提示评估期已过的解决办法 附序列号
- genymotion模拟器配置X86模拟器加速器
- 用JavaScript实现图片剪切效果
- kettle 在javascrip代码组件中使用fireToDB()函数实现自定义数据库查询
- 深入理解 Python yield
- word2vec详解与实战
- (转)live555学习笔记-UsageEnvironment和TaskScheduler
- 微信小程序之for循环
- Spring-boot简单的理解
热门文章
- &;amp与&;
- 强大的Core Image框架,各种滤镜处理图像
- POJ 1286 Necklaces of Beads (Burnside定理,有限制型)
- 关于Weblogic 10.3.1集群及调优经历
- CentOS 忘记root密码,解决方法
- 关于浮动float属性和position:absolute属性的区别
- C# ToString常用技巧总结
- Javascript闭包函数快速上手
- VS2010 release 和 debug 调试区别
- jQuery height()、innerHeight()、outerHeight()函数的区别