Android SharedPreferences(信息的保存和读取)


//main.xml,主布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/name" />  
  11.     <EditText  
  12.         android:id="@+id/webname"   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"          
  15.         />  
  16.     <TextView  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="@string/age"  
  20.         >  
  21.     </TextView>  
  22.     <EditText   
  23.         android:id="@+id/age"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         />  
  27.     <LinearLayout   
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:orientation="horizontal"  
  31.         >  
  32.           
  33.               
  34.         <Button   
  35.             android:id="@+id/saveButton"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="@string/save"  
  39.             />  
  40.             <Button   
  41.             android:id="@+id/restorationData"  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:text="@string/huifu"  
  45.             />  
  46.       
  47.     </LinearLayout>  
  48. </LinearLayout>  
//strings.xml常量字符串
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="hello">Hello SharedPreferencesActivity!</string>  
  5.     <string name="app_name">软件参数保存</string>  
  6.     <string name="name">网名</string>  
  7.     <string name="age">年龄</string>  
  8.     <string name="save">保存参数</string>  
  9.     <string name="success">保存成功</string>  
  10.     <string name="huifu">恢复参数</string>  
  11.   
  12. </resources>  
//SharedPreferencesActivity.java 处理类
  1. package sn.len.sharedpreferences;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.SharedPreferences;  
  6. import android.content.SharedPreferences.Editor;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. public class SharedPreferencesActivity extends Activity   
  14. {  
  15.     private  EditText name=null;  
  16.     private EditText age=null;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)   
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         name=(EditText)findViewById(R.id.webname);  
  23.         age=(EditText)findViewById(R.id.age);  
  24.         Button saveButton=(Button)findViewById(R.id.saveButton);  
  25.         Button restorationButton=(Button)findViewById(R.id.restorationData);  
  26.         //写入信息   
  27.         saveButton.setOnClickListener  
  28.         (  
  29.                 new View.OnClickListener()  
  30.                 {  
  31.                     @Override  
  32.                     public void onClick(View v)   
  33.                     {  
  34.   
  35.                         String web_name=name.getText().toString();  
  36.                         String true_age=age.getText().toString();  
  37.                         SharedPreferences preferences=getSharedPreferences("softinfo",Context.MODE_WORLD_READABLE);  
  38.                         Editor edit=preferences.edit();  
  39.                         edit.putString("name", web_name);  
  40.                         edit.putInt("age",new Integer(true_age));  
  41.                         edit.commit();  
  42.                         Toast.makeText(SharedPreferencesActivity.this, R.string.success,Toast.LENGTH_LONG).show();  
  43.                           
  44.                     }  
  45.                 }  
  46.         );  
  47.         //读取信息   
  48.         restorationButton.setOnClickListener  
  49.         (  
  50.                 new View.OnClickListener()  
  51.                 {  
  52.                       
  53.                     @Override  
  54.                     public void onClick(View v)   
  55.                     {  
  56.                         SharedPreferences ferences=getSharedPreferences("softinfo",0);  
  57.                         String true_name=ferences.getString("name""");  
  58.                         int true_age=ferences.getInt("age"20);  
  59.                         name.setText(true_name);  
  60.                         age.setText(String.valueOf(true_age));  
  61.                     }  
  62.                 }  
  63.         );  
  64.     }  
  65. }  

//运行结果

点击保存按钮,成功后消除内容,再点恢复数据,也就是重新读取XML的值放在EditText中


//重其它App中,访问这个APP的信息,如下代码

  1. package sn.len.others;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5. import android.content.pm.PackageManager.NameNotFoundException;  
  6. import android.test.AndroidTestCase;  
  7. import android.util.Log;  
  8.   
  9. public class SharPreferencesSoftinfoTest extends AndroidTestCase   
  10. {  
  11.     private static final String TAG="SharePreferencesContent";  
  12.     public void testAccessSharedPreferences() throws NameNotFoundException  
  13.     {  
  14.         //通过本应该程序的上下文来建想要访问App的上下文对象   
  15.         //sn.len.sharedpreferences 存放softinfo.xml那个包名称   
  16.         //CONTEXT_IGNORE_SECURITY 取消跨App的的访问安全   
  17.         Context accessSharePreferences=getContext().createPackageContext("sn.len.sharedpreferences",Context.CONTEXT_IGNORE_SECURITY);  
  18.         SharedPreferences shared=accessSharePreferences.getSharedPreferences("softinfo", Context.MODE_PRIVATE);  
  19.         String name=shared.getString("name""");  
  20.         int age=shared.getInt("age"18);  
  21.         Log.i(TAG, "name:"+name+"age:"+age);  
  22.     }  
  23. }  

相关内容