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;
} }
Android少量数据保存之SharedPreferences接口实例的更多相关文章
- Android数据存储(1)少量数据保存之SharedPreferences接口实例
SharedPreferences数据保存主要是通过键值的方式存储在xml文件中 xml文件在data/此程序的包名/XX.xml 格式 <?xml version='1.0' encoding ...
- Android简易数据存储之SharedPreferences
Andorid提供了多种数据存储的方式,例如前面说到的“Android数据存储之SQLite的操作”是用于较复杂的数据存储.然而,如果有些简单的数据存储如果采用SQLite的方式的话会显得比较笨重.例 ...
- Android 一个Activity保存它自己的实例
一个Activity保存他自己的实例的作用是,在其他Activity中可以方便的调用该Activity里的方法. 我们可以使用一个静态的变量保存当前Activity的实例,并将其标志为private访 ...
- Android中数据存储之SharedPreferences
import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...
- Android之数据存储之SharedPreferences
SharedPreferences是以键值对形式存储数据,主要用于记录系统的设置,如飞行模式是否开启,声音大小的值等.//SharedPreferences方式保存到xml文件SharedPrefer ...
- Android吧数据保存成xml文件
public class MainActivity extends Activity { private List<Person> persons; @Override protected ...
- Android数据存储之SharedPreferences存储
安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...
- Android中的数据保存
形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...
- Android数据保存之SharedPreference
前言: 程序中处理的大部分问题都与数据有关,读取数据显示在UI上,读取的数据可以是本地的,也可以是网络的.保存用户数据到存储空间,可以是本地的数据库,文件等,也可以是保存到网络服务器.总之大部分的程序 ...
随机推荐
- app图标和启动页设置
弄了一下午,终于把iOS中图标的设置和启动页的设置弄明白了.我想以后再也不会浑了. 进入正题: 一:apple 1).iPhone4s 3.5寸屏,也就是640*960,但在模拟器上正常用的是320* ...
- Ojbect-C2 3、NSArray和NSMutableArray数组的使用
Adopted Protocols NSCoding encodeWithCoder: initWithCoder: NSCopying copyWithZone: NSMutableCopying ...
- WF学习笔记(一)
-流程启动方式1: WorkflowInvoker.Invoke(new Workflow1()); -流程启动方式2: WorkflowApplication instance = new Work ...
- .Net用js实现aspx页面删除TextBox输入框的前后空格
去掉TextBox输入框两头的前后空格:onblur="this.value=this.value.replace(/^\s+|\s+$/g,'');" str为要去除空格的字符串 ...
- TabControl控件中TabPage的显示和隐藏
TabPage里面含有方法Hide和Show,但没有任何作用,实际隐藏和显示需要使用如下2个方法 方法一:此方法比较简单 TabPageServo.Parent = Nothing //隐藏 Ta ...
- windows8.1 App中webView 使用定位
windows8.1的webview的网页中没有办法直接定位 要想定位比较费劲 查了好久才发现一个可行的办法 那就是通过后台代码获取位置信息 然后调用页面中已有的获取位置信息的JS方法 把位置信 ...
- js登录页面的 回车事件
js登录页面的 回车事件 js登录页面的 回车事件(2012-12-26 10:37:03)转载▼标签: jseventkey回车事件登录 分类: js.jquery //回车事件 第一种docum ...
- javascript定义类的方法总结
1.构造函数法 类是对象的模板,定义了对象共有的方法属性数据 等,在javascript中一个函数就是一个对象,也可以看做一个类的构造方法. 所以我们可以像以下方式定义类: //1.经典的构造方法 Q ...
- 安装search everything中文语言包
Everything 作为很多人的必备工具,特写这篇文章,一方面让想使用国外优秀软件的英语小白有一段过渡期,另一方面也为自己整理下资料.当然,这个可不是不学好英语的正当理由. 步骤: 1. 下载好se ...
- nopcommerce 二次开发
http://www.cnblogs.com/nopcommerce-b2c/ http://www.nopchina.net/ 数据库结构 http://www.xcode.me/open/docu ...