您的位置:首页 > 移动开发 > Android开发

Android中SharedPreferences的用法(一)

2015-08-02 23:48 579 查看
有些时候,应用程序有少量的数据需要保存,而且这些数据的格式很简单:都是普通的字符串、标量类型的值等(基本数据类型),比如应用程序的各种配置信息(是否打开音效、是否使用振动)、小游戏的玩家积分(扫雷)等。对于这种数据,Android提供了SharedPreferences进行保存。SharedPreferences数据可供本应用程序使用,也可供其他应用程序共享,此处先介绍供本程序使用的方法。

SharedPreferences使用步骤

1、获取SharedPreferences对象:
getSharedPreferences(String name,int mode)
,该方法由Context提供。

name:SharedPreferences文件的名字

mode:有三种模式(1)
Context.MODE_PRIVATE
(SharedPreferences数据只能被本应用程序读写);(2)
Context.MODE_WORLD_READABLE
(能被其他应用程序读);(3)
Context.MODE_WORLD_WRITEABLE
(可以被其他应用程序读写)

2、读出数据:使用
xxx getXxx(String key,xxx default)
方法获取指定key对应的value,如果不存在则返回默认值。其中xxx可以是boolean、float、int、long、String等各种基本类型。

其他方法:

boolean contains(String key)
:判断SharedPreferences是否包含特定key的数据。

abstract Map<String,?> getAll()
:获取所有的key-value对。SharedPreferences的根元素是
<map.../>


3、写入数据:

(1)获取SharedPreferences内部接口Editor:
SharedPreferences.Editor editor = (SharedPreferences对象).edit();


(2)
SharedPreferences.Editor.put(String key,xxx value)
:存入指定key对应的数据,其中xxx可以是boolean、float、int、long、String等各种基本类型。

(3)
SharedPreferences.Editor.commit()
:提交Editor的所有修改

其他方法:

SharedPreferences.Editor.clear()
:清空所有数据

SharedPreferences.Editor.remove(String key)
:删除指定key对应的数据

以下为一个读写SharedPreferences数据的小示例,项目名为DemoSharedPreferences

activity_main.xml布局文件

<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"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:gravity="center"
android:text="SharedPreferences读写本应用数据测试" />

<Button
android:id="@+id/bt_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入数据" />

<Button
android:id="@+id/bt_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据" />

</LinearLayout>


com.example.demosharedpreferences.MainActivity.java代码

package com.example.demosharedpreferences;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button bt_write,bt_read;
private SharedPreferences sp;
private SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt_write = (Button) findViewById(R.id.bt_write);
bt_read = (Button) findViewById(R.id.bt_read);
//      获取SharedPreferences对象,文件名为preferences,模式为仅供本应用程序使用
sp = getSharedPreferences("preferences", MODE_PRIVATE);
//      获取SharedPreferences.Editor对象
editor = sp.edit();

bt_write.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");
//              存入当前时间
editor.putString("time", sdf.format(new Date()));
//              存入一个随机数
editor.putInt("random", (int)(Math.random()*100));
//              提交所有存入的数据
editor.commit();
}
});

bt_read.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//              读取字符串数据
String time = sp.getString("time", null);
//              读取int类型数据
int randNum = sp.getInt("random", 0);
String result = time==null?"您暂时还未写入数据"
:"写入时间:" + time
+ "\n上次生成的随机数为:" + randNum;
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: