SharedPreferences is the easiest data storage technology to understand in Android . In fact, SharedPreferences deals with a key-value (key-value pair). SharedPreferences are often used to store some lightweight data.
Write data to SharedPreferences
1. Use getSharedPreferences(fileName, MODE_PRIVATE) to get the SharedPreferences object
2. Obtain the SharedPreferences.Editor object through the SharedPreferences object
3. Store data in the form of key-value pairs through SharedPreferences.Editor
4. Call SharedPreferences.Editor's commit() to submit the stored data.
Read data from SharedPreferences
1. Use getSharedPreferences(fileName, MODE_PRIVATE) to get the SharedPreferences object
2. Use sharedPreferences.getString("name", "") to get the value of value The second parameter is the default value of value
The following examples illustrate:
Project name: ShareData
Here is the file description:
SavaData.java is used to store files, activity_save.xml is its layout file, when click save
When the file button is pressed, the information is saved in share.xml
LoadData.java is used to read data from share.xml file and display the content, activity_load.xml is
layout file
Note: When clicking the display data button, it will jump from SaveActivity---> into LoadActivity (implemented using Intent,
Introduced in the previous article)
The following is the layout map and source code:
activity_save.xml source code:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.lh.share.SaveActivity" > <Button android:id="@+id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/savebtn" /> <Button android:id="@+id/showbtn" android:layout_toRightOf="@id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/showbtn" /> </RelativeLayout>
SaveData.java source code:
package cn.lh.share; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SaveActivity extends Activity { private Button showbtn = null; private Button savebtn = null; //Define file name private static final String FILENAME = "share"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_save); showbtn = (Button)findViewById(R.id.showbtn); showbtn.setOnClickListener(new showListener()); savebtn = (Button)findViewById(R.id.savebtn); savebtn.setOnClickListener(new productListener()); } public class productListener implements OnClickListener{ @Override public void onClick(View v) { //Specify the name of the operation file SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE); //edit file Editor edit = share.edit(); // save the content edit.putString("author", "LuHua"); edit.putInt("age", 26); //update file edit.commit(); } } public class showListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(SaveActivity.this, LoadActivity.class); startActivity(intent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.save, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
activity_load.xml source code:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.lh.share.SaveActivity" > <TextView android:id="@+id/authorinfo" android:textSize="22sp" android:textColor="#ff00ff" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/ageinfo" android:layout_below="@id/authorinfo" android:textSize="22sp" android:textColor="#ffff00" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
LoadData.java source code:
package cn.lh.share; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; public class LoadActivity extends Activity { private static final String FILENAME = "share"; private TextView authorinfo = null; private TextView ageinfo = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate (savedInstanceState); super.setContentView(R.layout.activity_load); authorinfo = (TextView)findViewById(R.id.authorinfo); ageinfo = (TextView)findViewById(R.id.ageinfo); //Specify the operation file name SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE); authorinfo.setText("Author:"+share.getString("author", "No author information")); ageinfo.setText("年龄:"+share.getInt("age", 0)); } }
Finally, let's talk about the location where the generated share.xml file is saved
The file is saved in DDMS, you can select Window->Open Perspective->Other command to open
DDMS
After opening, select File Explorer\data\data\<package name>\shared_prefs, as shown below:
You can also export the generated file, first select the share.xml file, and then click Pull a in the DDMS toolbar
file from the device button, as shown below: