Android中5大数据存储(一)---SharedPreferences存储
来源:互联网 发布:无限网络 封优酷 编辑:程序博客网 时间:2024/05/29 06:52
在实际的软件运行中,往往需要许多配置参数信息,如Windows操作系统的引导文件boot.ini就保存了操作系统的配置参数,在编写JavaSE或JavaEE时,也往往会使用资源文件(*.properties)保存一些系统的而配置信息,而在Android中,如果想要实现配置信息的保存则需要使用SharedPreferences完成。
SharedPrrferences提供了一些基础的信息保存功能,所有的信息都是安装“key=value”的形式进行保存的,但是android.content,SharedPreferences接口所保存的信息只能是一些基本的数据类型,如字符串、整型、布尔型等。
下面举例说明:
项目名称:ShareData
以下是文件说明:
SavaData.java用来实现文件的存储,activity_save.xml为其布局文件,当点击保存文件按钮时,信息保存在share.xml
LoadData.java用来从share.xml文件读取数据,并将内容显示,activity_load.xml为布局文件
注意:当点击显示数据按钮会从SaveActivity--->跳入LoadActivity(使用Intent实现,前面文章有介绍)
下面依次是布局贴图和源码:
activity_save.xml源码:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.lh.share.SaveActivity" > <Button android:id="@+id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/savebtn" /> <Button android:id="@+id/showbtn" android:layout_toRightOf="@id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/showbtn" /> </RelativeLayout>
SaveData.java源码:
package cn.lh.share;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SaveActivity extends Activity {private Button showbtn = null;private Button savebtn = null;//定义文件名称private static final String FILENAME = "share"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_save); showbtn = (Button)findViewById(R.id.showbtn); showbtn.setOnClickListener(new showListener()); savebtn = (Button)findViewById(R.id.savebtn); savebtn.setOnClickListener(new productListener()); } public class productListener implements OnClickListener{@Overridepublic void onClick(View v) {//指定操作文件的名称SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE);//编辑文件Editor edit = share.edit();//保存内容edit.putString("author", "LuHua");edit.putInt("age", 26);//更新文件edit.commit();} } public class showListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(SaveActivity.this, LoadActivity.class);startActivity(intent);} } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.save, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
activity_load.xml源码:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.lh.share.SaveActivity" > <TextView android:id="@+id/authorinfo" android:textSize="22sp" android:textColor="#ff00ff" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/ageinfo" android:layout_below="@id/authorinfo" android:textSize="22sp" android:textColor="#ffff00" android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>
LoadData.java源码:
package cn.lh.share;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.widget.TextView;public class LoadActivity extends Activity {private static final String FILENAME = "share";private TextView authorinfo = null;private TextView ageinfo = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);super.setContentView(R.layout.activity_load);authorinfo = (TextView)findViewById(R.id.authorinfo);ageinfo = (TextView)findViewById(R.id.ageinfo);//指定操作文件名称SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE);authorinfo.setText("作者:"+share.getString("author", "没有作者信息"));ageinfo.setText("年龄:"+share.getInt("age", 0));}}
最后说下生成的share.xml文件保存的位置
文件被保存在DDMS中,可以选择Window->Open Perspective->Other命令,打开DDMS
打开之后选择 File Explorer\data\data\<package name>\shared_prefs,如下图:
也可以将生成的文件导出,首先选中share.xml文件,再单击DDMS工具栏中的Pull a file from the device按钮,如下图:
- Android中5大数据存储(一)---SharedPreferences存储
- Android数据存储(一):SharedPreferences存储
- Android中SharedPreferences数据存储
- Android中的5中数据存储方式之SharedPreferences存储
- android数据存储(一)之SharedPreferences
- Android数据存储(一):SharedPreferences
- Android数据存储(一)----SharedPreferences详解
- Android数据存储(一)----SharedPreferences详解
- Android数据存储(一)----SharedPreferences详解
- Android中的数据存储(一):SharedPreferences
- Android 数据存储 (一)SharedPreferences
- Android数据存储(一)---SharedPreferences
- Android 数据存储笔记一 SharedPreferences数据存储
- Android数据存储-SharedPreferences
- Android数据存储---SharedPreferences
- Android数据存储---SharedPreferences
- android SharedPreferences数据存储
- Android 数据存储 -- SharedPreferences
- 统计难题
- LeetCode题解-6. ZigZag Conversion
- 关于代理、通知和KVO区别
- Valid Parentheses
- python show-me-the-code 第0004
- Android中5大数据存储(一)---SharedPreferences存储
- 在vs2008中怎么把一个项目复制到另一个项目中
- maven 快照
- 找出21位的水仙数
- Android 自定义流式布局
- java代码实现mongodb故障迁移和读写分离
- Jsp到Action后台的数据接收方法
- Runtime运行时机制
- 二叉树java版