Android之SharedPreference轻量级数据存储

android的配置文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.android_sharedpreference"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="9" />  
  10.     <instrumentation android:targetPackage="com.example.android_sharedpreference" android:name="android.test.InstrumentationTestRunner"></instrumentation>  
  11.   
  12.     <application  
  13.         android:allowBackup="true"  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name"  
  16.         android:theme="@style/AppTheme" >  
  17.         <uses-library android:name="android.test.runner"></uses-library>  
  18.         <activity  
  19.             android:name="com.example.android_sharedpreference.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

下面是操作代码

[java]  view plain copy
  1. package com.example.android_sharedpreference;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import android.content.Context;  
  7. import android.content.SharedPreferences;  
  8.   
  9. public class MySharedpreference {  
  10.     private Context context;  
  11.   
  12.     public MySharedpreference(Context context) {  
  13.         // TODO Auto-generated constructor stub  
  14.         this.context = context;  
  15.     }  
  16.     public boolean saveMessage(String name,String pswd){  
  17.         boolean flag=false;  
  18.           
  19.         SharedPreferences sharedPreferences=context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);  
  20.         //对数据进行编辑  
  21.         SharedPreferences.Editor editor=sharedPreferences.edit();  
  22.           
  23.         editor.putString("name",name);  
  24.         editor.putString("pswd", pswd);  
  25.         flag=editor.commit();//将数据持久化  
  26.         return flag;  
  27.     }  
  28.   
  29.     public Map<String,Object>getMessage(){  
  30.         Map<String, Object>map=new HashMap<String, Object>();  
  31.         SharedPreferences sharedPreferences=context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);  
  32.         String name=sharedPreferences.getString("name","");  
  33.         String pswd=sharedPreferences.getString("pswd""");  
  34.         map.put("name", name);  
  35.         map.put("pswd",pswd);  
  36.         return map;  
  37.           
  38.     }  
  39. }  

测试代码

[java]  view plain copy
  1. import java.util.Map;  
  2.   
  3. import android.content.Context;  
  4. import android.test.AndroidTestCase;  
  5. import android.util.Log;  
  6.   
  7. public class MyTest extends AndroidTestCase {  
  8.       
  9.     private String TAG="MyTest";  
  10.       
  11.     public void save(){  
  12.         Context context=getContext();  
  13.           
  14.         MySharedpreference mySharedpreference=new MySharedpreference(context);  
  15.         boolean flag=mySharedpreference.saveMessage("admin""password");  
  16.         Log.i(TAG,"-->"+flag);  
  17.           
  18.     }  
  19.       
  20.     public void find(){  
  21.     Context context=getContext();  
  22.     MySharedpreference mySharedpreference=new MySharedpreference(context);  
  23.       Map<String, Object> map=mySharedpreference.getMessage();  
  24.       Log.i(TAG, map.toString());  
  25.     }  
  26.       
  27.   
  28. }  

在Android系统中,  其配置文件的数据文件  以XML文件的形式保存在  /data/data/目录  /shared_prefs  目录下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值