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接口实例的更多相关文章

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

    SharedPreferences数据保存主要是通过键值的方式存储在xml文件中 xml文件在data/此程序的包名/XX.xml 格式 <?xml version='1.0' encoding ...

  2. Android简易数据存储之SharedPreferences

    Andorid提供了多种数据存储的方式,例如前面说到的“Android数据存储之SQLite的操作”是用于较复杂的数据存储.然而,如果有些简单的数据存储如果采用SQLite的方式的话会显得比较笨重.例 ...

  3. Android 一个Activity保存它自己的实例

    一个Activity保存他自己的实例的作用是,在其他Activity中可以方便的调用该Activity里的方法. 我们可以使用一个静态的变量保存当前Activity的实例,并将其标志为private访 ...

  4. Android中数据存储之SharedPreferences

    import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...

  5. Android之数据存储之SharedPreferences

    SharedPreferences是以键值对形式存储数据,主要用于记录系统的设置,如飞行模式是否开启,声音大小的值等.//SharedPreferences方式保存到xml文件SharedPrefer ...

  6. Android吧数据保存成xml文件

    public class MainActivity extends Activity { private List<Person> persons; @Override protected ...

  7. Android数据存储之SharedPreferences存储

    安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...

  8. Android中的数据保存

    形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...

  9. Android数据保存之SharedPreference

    前言: 程序中处理的大部分问题都与数据有关,读取数据显示在UI上,读取的数据可以是本地的,也可以是网络的.保存用户数据到存储空间,可以是本地的数据库,文件等,也可以是保存到网络服务器.总之大部分的程序 ...

随机推荐

  1. ASP.NET 导入EXCEL文档

    鉴于教务一般都是手动输入学生信息,在未了解本校数据库的客观情况之下,我们准备设计一个导入excel文档中学生信息如数据库的功能.结合网上各类大牛的综合版本出炉.. 首先具体的实现思想如下: 1.先使用 ...

  2. C#操作数据库,将其查查出来的记录条数显示在winform窗体中的方法之一

    //1.数据库链接的基本操作(略) //2.创建对象函数(关键部分) sqlConn.Open(); //初始化定义记录条数 ; object obj = sqlComm.ExecuteScalar( ...

  3. CSS布局模型思考

    flow模型:默认布局模型,元素从左向右.从上到下依次排列,块状元素独占一行.Position属性对应值static. float模型:主要效果是让本来独占一行的块状元素变成内联-块状元素,并到一排显 ...

  4. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  5. 使用Python操作Redis

    1. 安装pyredis 首先安装pip   1 2 3 4 5 6 7 8 <SHELL># apt-get install python-pip ...... <SHELL> ...

  6. python类class基础

              44.class类:                      一.类定义的一般形式:                            1.简单的形式:实例化对象没有自己独有 ...

  7. IOS 企业版证书($299)In-House方式发布指南

    一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...

  8. 开发Nginx模块

    开发Nginx模块 前面的哪些话 关于Nginx模块开发的博客资料,网上很多,很多.但是,每篇博客都只提要点,无法"step by step"照着做,对于初次接触Nginx开发的同 ...

  9. laravel框架——线上环境错误总结

    除了根目录,其他目录访问全是Not Found

  10. How to use System.Diagnostics.Process correctly

    I’ve seen many a question on stackoverflow and other places about running a process and capturing it ...