歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android SharedPreferences(信息的保存和讀取)

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

日期:2017/3/1 10:51:22   编辑:Linux編程

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

//運行結果

點擊保存按鈕,成功後消除內容,再點恢復數據,也就是重新讀取XML的值放在EditText中


//重其它App中,訪問這個APP的信息,如下代碼

  1. package sn.len.others;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.pm.PackageManager.NameNotFoundException;
  5. import android.test.AndroidTestCase;
  6. import android.util.Log;
  7. public class SharPreferencesSoftinfoTest extends AndroidTestCase
  8. {
  9. private static final String TAG="SharePreferencesContent";
  10. public void testAccessSharedPreferences() throws NameNotFoundException
  11. {
  12. //通過本應該程序的上下文來建想要訪問App的上下文對象
  13. //sn.len.sharedpreferences 存放softinfo.xml那個包名稱
  14. //CONTEXT_IGNORE_SECURITY 取消跨App的的訪問安全
  15. Context accessSharePreferences=getContext().createPackageContext("sn.len.sharedpreferences",Context.CONTEXT_IGNORE_SECURITY);
  16. SharedPreferences shared=accessSharePreferences.getSharedPreferences("softinfo", Context.MODE_PRIVATE);
  17. String name=shared.getString("name", "");
  18. int age=shared.getInt("age", 18);
  19. Log.i(TAG, "name:"+name+"age:"+age);
  20. }
  21. }
Copyright © Linux教程網 All Rights Reserved