Android serialization 17 – use shared preference to save and read data

Time:2020-10-14

1、 There are three ways to get the sharedpreference object:

  • The first method has been serialized last time and will not be repeated this time
  • The second way: use the getpreference () method in activity
    • This method is very similar to the getsharedprences() method in context, but it only accepts one operation mode parameter, because when using this method, the currently active class name will be automatically used as the file name of sharedpreference.
  • The third way: use the getdefaultsharedpreference() method in the preferencemanager class
    • This is a static method that takes a context parameter and automatically names the sharedpreference file with the enrollment in the current application as a prefix.

1. Get the given object, and then store the object in the shared preference file

  • (1) Call the edit() method of the sharedperference object to get a SharedPreferences.Editor Object;
  • (2) To SharedPreferences.Editor To add data to an object, for example, to add a boolean type is to use the putboolean method. To add a string of data, you can use the putstring method, and so on;
  • (3) The commit() method is called to submit the added data to complete the data storage operation.

2. New project belowSharedPreferenceTestProject to test

  • modifyactivity_main.xmlfile
  • Do not do any complex functions, here only define a button to submit data, and then modify the code in mainactivity
package com.example.sharedpreferencetest;

import android.app.Activity;
import android.content.SharedPreferences;
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 MainActivity extends Activity {
	
	private Button saveData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		saveData = (Button)findViewById(R.id.save_data);
		saveData.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
				editor.putString("name","Tom");
				editor.putInt("age", 25);
				editor.putBoolean("married",false);
				editor.commit();
			}
		});		
	}
}
  • The button “click” is registered with an event, and the other steps are consistent with the above description.
    31.1

  • Run the program, click a button, and then view it/data/data/com.example.sharedpreferencestest/shard_prefsIn the directory, you can see that adata.xmlFile, export it to view
    31.2
    31.3

3. Read data from SharedPreferences

  • Just like storing data, there is a corresponding method for reading data. If you store get and data type, then reading is get plus data type. There are two parameters for these methods: one is the key of data, the other is the default value if the key is not found.
  • stayactivity_main.xmlAdd the following code to the
  • Add a restore button, and then we register the activity of this button
  • stayMainActivity.javaAdd the following code to the
restoreData = (Button)findViewById(R.id.restore_data);
		restoreData.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
				String name = pref.getString("name","");
				int age = pref.getInt("age", 0);
				boolean married = pref.getBoolean("married",false);
				Log.d("MainActivity","name is "+ name);
				Log.d("MainActivity","age is "+ age);
				Log.d("MainActivity","marrid is "+ married);
			}
		});
  • Log through the log log

3、 Source code:

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]