SharedPreferences
[功能]
大家在android开发中 一点有这样的需求 就是需要保存一下与该程序有关的属性设置的问题
比如:window xp 中 <假设系统盘为 C:/> 的位置为: C:\Program Files
那么在android中是怎样呢? 那就是:SharedPreferences
既然它是用来保存数据的 那么一点下面问题:
1. 如何创建
2. 如何加入数据
3. 如何取出数据
因为 很多程序都有这个需要 所以自己把该功能集成并列出一些接口函数 以后用的话 会方便很多 这个类名为:SharedPreferencesHelper
[代码]
1. 以指定名字来创建
SharedPreferences sp;
SharedPreferences.Editor editor;
Context context;
public SharedPreferencesHelper(Context c,String name){
context = c;
sp = context.getSharedPreferences(name, 0);
editor = sp.edit();
}
2. 以键值<String Key,String Value> 的方式加入数据
public void putValue(String key, String value){
editor = sp.edit();
editor.putString(key, value);
editor.commit();
}
3. 以 String Key 为索引来取出数据
public String getValue(String key){
return sp.getString(key, null);
}
4. 如何使用 SharedPreferencesHelper
package com.android.SharedPreferences;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/*
* to access from: data/data/com.android.SharedPreferences/share_prefs
*/
public class SharedPreferencesUsage extends Activity {
public final static String COLUMN_NAME ="name";
public final static String COLUMN_MOBILE ="mobile";
SharedPreferencesHelper sp;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
sp = new SharedPreferencesHelper(this, "contacts");
//1. to store some value
sp.putValue(COLUMN_NAME, "Gryphone");
sp.putValue(COLUMN_MOBILE, "123456789");
//2. to fetch the value
String name = sp.getValue(COLUMN_NAME);
String mobile = sp.getValue(COLUMN_MOBILE);
TextView tv = new TextView(this);
tv.setText("NAME:"+ name + "\n" + "MOBILE:" + mobile);
setContentView(tv);
}
}
5. 其他问题
* 文件存放路径: 因为我的这个例子的pack_name 为:package com.android.SharedPreferences;
所以存放路径为:data/data/com.android.SharedPreferences/share_prefs/contacts.xml
* contacts.xml 的内容为:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="mobile">123456789</string>
<string name="name">Gryphone</string>
</map>
* 取出的数据为:
分享到:
相关推荐
项目aminiTwitter源码部分,多谢下载,如果有误或者更好的代码希望与我消息哦,鄙人必定感激不尽,泪流如下。
标题"Android-SharedPreferences-小小例子"指的是通过一个小型的Android应用来演示`SharedPreferences`的使用。这个应用可能包含了创建、读取和更新用户偏好设置的功能,以便用户在不同的会话之间保持特定的配置。 ...
SharedPreferences是Android提供的一个轻量级存储类,经常用于保存软件设置参数。存放的格式为xml,文件存放在 /data/data/<package name>/shared_prefs下。
Android-SharedPreferences-Helper 简化默认Android SharedPreferences类的用法。 库大小=〜15kb。 开发人员可以执行几行代码,否则将需要几行代码。 与默认类相比,它易于理解,并且易于使用。突出特点资料库大小:...
其中,SharedPreferences是Android提供的一种轻量级的数据存储方式,它主要用于存储简单的键值对数据,如布尔值、整型、浮点型、字符串等。本篇文章将深入探讨SharedPreferences的工作原理、使用方法以及相关示例。 ...
SharedPreferences是Android提供的一种轻量级的数据存储方式,主要用于存储少量简单的键值对数据,如配置设置、用户偏好等。在本篇博文中,我们将深入探讨如何使用SharedPreferences进行数据存储,并通过源码分析其...
在Android开发中,SharedPreferences是系统提供的一种轻量级的数据存储方式,主要用于保存应用程序中的简单配置数据,如布尔值、整型、浮点型、字符串等。由于它的操作简便且易于理解,因此在需要持久化小量数据时...
在开始使用SharedPreferences之前,你需要确保在项目的build.gradle文件中添加了Android SDK的相关依赖。通常情况下,Android Studio已经默认包含了这个库,无需额外添加。 2. **获取SharedPreferences对象** 要...
一个SharedPreferences工具类 在工程根目录build.gradle添加jitpack: allprojects { repositories { maven { url "https://jitpack.io" } } } 在使用AnyPref的模块中添加: dependencies { compile '...
在Android应用开发中,SharedPreferences是Android提供的一种轻量级的数据存储方式,主要用于存储一些简单的配置信息,如用户偏好设置、应用状态等。它基于XML文件,提供了键值对的存储方式,非常适合用来保存一些...
在Android应用开发中,数据库和SharedPreferences是两种常用的数据存储机制。`Android-Debug-Database`是一个专门为Android开发者设计的库,旨在简化调试过程,帮助开发者更有效地检查和修改应用程序中的数据库和...
android应用SharedPreferences_Demoandroid应用SharedPreferences_Demoandroid应用SharedPreferences_Demoandroid应用SharedPreferences_Demoandroid应用SharedPreferences_Demoandroid应用SharedPreferences_Demo...
在Android应用开发中,SharedPreferences通常用于存储轻量级的数据,如用户设置或应用程序的状态。而使用Kotlin和RxJava(一种响应式编程库)来增强SharedPreferences的管理,可以提供更流畅、异步友好的方式来读写...
Optional persistent cookie store, saves cookies into your app's SharedPreferences Examples For inspiration and testing on device we've provided Sample Application. See individual samples here on ...
在Android开发中,SharedPreferences是应用中常用的一种轻量级数据存储方式,用于保存简单的键值对数据,如布尔值、整数、浮点数、字符串等。然而,当多个进程同时访问同一份SharedPreferences时,可能会遇到数据...
**Android的SharedPreferences详解** SharedPreferences是Android系统提供的一种轻量级的数据存储方式,它主要用于应用程序中简单数据的持久化存储,例如用户设置、应用状态等。SharedPreferences采用键值对的形式...
android,SharedPreferences
【Android中的DataStore替代SharedPreferences】 Android开发者们长期以来一直依赖SharedPreferences作为轻量级的数据存储解决方案,它的简单易用性使其在小规模数据存储方面受到了广泛欢迎。然而,随着应用程序...
Android Support Library v4包含了大量的实用工具类和接口,如SharedPreferences用于持久化数据,Loader管理异步加载,Fragment处理界面组件的生命周期等。这些功能在Unity游戏与Android系统之间建立桥梁,使得...