exampleuse SharedPreferences

简介:   exampleuse.java /* * to access from: data/data/com.android.SharedPreferences/share_prefs */public class exampleuse extends Activity { public final static String COLUMN_NAME ="name"; publ

 

exampleuse.java

/*
 * to access from: data/data/com.android.SharedPreferences/share_prefs
 */
public class exampleuse extends Activity {
	public final static String COLUMN_NAME ="name";
	public final static String COLUMN_MOBILE ="mobile";
	
	exampleHelper sp;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        sp = new exampleHelper(this, "contacts");
        
        //1. to store some value
        sp.putValue(COLUMN_NAME, "Mr WANG");
        sp.putValue(COLUMN_MOBILE, "XXXXXXXXXX");
        
        
        //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);
    }
}

exampleHelper

public class exampleHelper {
	SharedPreferences sp;
	SharedPreferences.Editor editor;
	
	Context context;
	
	public exampleHelper(Context c,String name){
		context = c;
		sp = context.getSharedPreferences(name, 0);
		editor = sp.edit();
	}
	
	public void putValue(String key, String value){
		editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public String getValue(String key){
		return sp.getString(key, null);
	}
	
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>











目录
相关文章
|
6月前
|
存储 API 文件存储
47. 【Android教程】SharedPreferences 存储
47. 【Android教程】SharedPreferences 存储
74 2
|
5月前
SharedPreferences工具类
SharedPreferences工具类
36 0
SharedPreferences封装
SharedPreferences封装 存 取 封装类
261 0
|
存储 XML 安全
SharedPreferences VS MMKV
简单了解 Android 轻量级存储 SharedPreferences 和 MMKV!
832 0
SharedPreferences VS MMKV
|
存储 XML 数据格式
SharedPreferences源码解析
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ 1.简介 写这篇博客目的在于巩固自己对SharedPreferences的理解。
1210 0
|
XML 数据格式 Java
全面剖析SharedPreferences
1.原理和概述 1.储存于硬盘上的xml键值对。 2.轻量级数据储存,数据多了容易引起性能问题 3.xml文件所在目录位于/data/data//shared_prefs/,可以有多个文件。
990 0