博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android(三) 数据存储之二 SharedPreferences
阅读量:4067 次
发布时间:2019-05-25

本文共 4525 字,大约阅读时间需要 15 分钟。

在此之前的学习内容是数据存储之一文件存储。在本地存储中常用的有,文件、配置文件、数据库。前面的学习主要是针对本地文件的。我认为可以把SharedPreferences看做是配置文件,虽然它也是采用XML格式存储的。

比如我们使用的桌面软件中,通常会有一个“选项”菜单,选项是对软件的常规或核心设置。在Android中我们使用SharedPreferences来完成这种对配置文件的读写。在JavaSE和JavaEE中常用的是*.properties,在Windows平台下常使用*.ini文件。

下面,我们编写一个使用SharedPreferences读写配置文件的小例子。

1.创建Android工程

Project name:AndroidSharedPreferences

BuildTarget:Android2.1

Application name:Android 应用程序配置

Package name:com.changcheng.sharedpreferences

Create Activity:AndroidSharedPreferences

Min SDK Version:7

2.编辑strings.xml:

"1.0" encoding="utf-8"?>

 

"hello">Hello World, AndroidSharedPreferences!

"app_name">Android 应用程序配置

"tv_name">姓名

"tv_age">年龄

"bt_write">设置

"bt_read">读取

"save_success">保存成功

"save_failed">保存失败

 

3.编辑main.xml

"1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

"fill_parent"

android:layout_height="wrap_content">

"70dip" android:layout_height="wrap_content"

android:textSize="25dip" android:id="@+id/tv_name" android:text="@string/tv_name" />

"300dip"

android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_name"

android:id="@+id/et_name" />

 

"fill_parent"

android:layout_height="wrap_content">

"70dip" android:layout_height="wrap_content"

android:textSize="25dip" android:id="@+id/tv_age" android:text="@string/tv_age" />

"300dip"

android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_age"

android:id="@+id/et_age" />

 

"fill_parent"

android:layout_height="wrap_content" android:gravity="right">

 

 "wrap_content"

android:layout_height="wrap_content" android:text="@string/bt_write"

android:id="@+id/bt_set" />

 

 

"wrap_content"

android:layout_height="wrap_content" android:layout_toRightOf="@id/bt_set"

android:text="@string/bt_read" android:id="@+id/et_read" />

 

 

4.为按钮添加事件代码

package com.changcheng.sharedpreferences;

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class AndroidSharedPreferences extends Activity {

private static final String TAG = "AndroidSharedPreferences";

private EditText etName;

private EditText etAge;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 获取按钮

Button btSet = (Button) this.findViewById(R.id.bt_set);

Button btRead = (Button) this.findViewById(R.id.bt_read);

// 获取编辑框

etName = (EditText) this.findViewById(R.id.et_name);

etAge = (EditText) this.findViewById(R.id.et_age);

// 添加事件

btSet.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 获取名称和年龄

String name = etName.getText().toString();

String age = etAge.getText().toString();

// 创建SharedPreferences

SharedPreferences sp = getSharedPreferences("preferences",

Context.MODE_PRIVATE);

// 添加数据

Editor editor = sp.edit();

editor.putString("name", name);

editor.putInt("age", Integer.parseInt(age));

// 保存数据

if (editor.commit())

Toast.makeText(AndroidSharedPreferences.this,

R.string.save_success, 1).show();

else

Toast.makeText(AndroidSharedPreferences.this,

R.string.save_failed, 1).show();

}

});

btRead.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 创建SharedPreferences

SharedPreferences sp = getSharedPreferences("preferences",

Context.MODE_PRIVATE);

// 获取数据

String name = sp.getString("name", "defName");

String age = sp.getInt("age", 0) + "";

// 显示数据

etName.setText(name);

etAge.setText(age);

}

});

}

}

5.运行程序

启动模拟器,运行程序。输入名称和年龄,点击保存。我们使用的代码是getSharedPreferences("preferences",Context.MODE_PRIVATE);,当然commit时。它会为我们为”/data/data/com.changcheng.sharedpreferences/shared_prefs/preferences.xml”。将 preferences.xml导出,查看它的内容为:

 

 

长城

 

 

将名称和年龄编辑框的内容清空,然后点击读取按钮,刚才写出的内容被读取进来。 SharedPreferences的使用就是这么简单。

6.其他程序访问本程序的配置

通过SharedPreferences创建的配置文件,不需要指定路径和文件后缀名,读取的时候也是。通常情况下,配置只是提供给本应用程序使用的。在这里我们介绍一个小知识点,即其他程序想使用本应用程序的配置,那应该如何使用SharedPreferences呢?如下:

Context otherAppContext = createPackageContext("com.changcheng.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);

SharedPreferences sharedPreferences = otherAppContext.getSharedPreferences("preferences", Context.MODE_WORLD_READABLE);

注意,为了使其他程序可以访问本应用程序的配置。那么在我们使用 getSharedPreferences创建配置的时候必须为它的文件访问模式设置为允许其他程序读取或写入等。

  转载请标明出处  

技术交流群:173711587

你可能感兴趣的文章
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
ORACLE权限管理调研笔记
查看>>
移进规约冲突一例
查看>>
IA32时钟周期的一些内容
查看>>
SM2椭圆曲线公钥密码算法
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>