Android source code analysis (14) -- how to save data with SharedPreferences

Posted by amarafzal on Thu, 02 Jan 2020 11:34:43 +0100

1: How to use SharedPreference s

This article only provides a way to save data. Please adjust the specific use scenarios according to the needs.
EditText add save data click event to save data.

diff --git a/res/layout/activity_voicemail_settings.xml b/res/layout/activity_voicemail_settings.xml
index cd7af05..57526f2 100755
--- a/res/layout/activity_voicemail_settings.xml
+++ b/res/layout/activity_voicemail_settings.xml
@@ -29,6 +29,7 @@
         android:imeOptions="flagForceAscii|actionDone"
         android:includeFontPadding="true"
         android:inputType="text"
+        android:onClick="saveData"
         android:paddingLeft="@dimen/focus_item_padding_x"
         android:singleLine="true"
         android:textColor="@color/lv_focused"
  1. Get the SharedPreferen ces object through getsharedpreferences ("voicemail? Data, mode? Private").
  2. sharedPreferences.edit(); gets the editor object.
  3. initData() initializes EditText data.
  4. saveData() saves the input data.
  5. Mselecttext. Setselection (msedittext. Gettext(). Length()); this method mainly sets the EditText cursor position.
diff --git a/src/com/android/settings/VoicemailSettingsActivity.java b/src/com/android/settings/VoicemailSettingsActivity.java
index 9babb18..58e929a 100755
--- a/src/com/android/settings/VoicemailSettingsActivity.java
+++ b/src/com/android/settings/VoicemailSettingsActivity.java
@@ -4,6 +4,7 @@ import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.os.Bundle;
@@ -12,6 +13,7 @@ import android.view.KeyEvent;
 import android.view.View;
 import android.view.Window;
 import android.widget.EditText;
+import android.widget.Toast;

 import java.util.List;

@@ -22,6 +24,10 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
     private int mSettingsFocusHeight;
     private int mSettingsNormalHeight;
     private int mSettingsStartY;
+    private SharedPreferences sharedPreferences;
+    private SharedPreferences.Editor editor;
+    private String key;
+    private String result;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -33,7 +39,12 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
         for (int i = 0; i < ids.length; i++) {
             strs[i] = getString(ids[i]);
         }
+        sharedPreferences = getSharedPreferences("voicemail_data", MODE_PRIVATE);
+        editor = sharedPreferences.edit();
         mEditText = (EditText) findViewById(R.id.input_number);
+        initData();
+        mEditText.setSelection(mEditText.getText().length());//Cursor end
         mSettingsRecyclerView = (SettingsRecyclerView) findViewById(R.id.voicemail_list_view);
         mSettingsAdapter = new SettingsAdapter(this, strs, this);
         mSettingsRecyclerView.setLayoutManager(mSettingsAdapter.getLayoutManager());
@@ -45,6 +56,25 @@ public class VoicemailSettingsActivity extends Activity implements View.OnClic
         mSettingsRecyclerView.setHasFixedSize(true);
     }

+    private void initData() {
+        result = sharedPreferences.getString(key, null);
+        if (result == null) {
+            mEditText.setText("");
+        } else {
+            mEditText.setText(result);
+        }
+    }
+
+    public void saveData(View view) {
+        editor.remove(key);
+        String value = mEditText.getText().toString().trim();
+        editor.putString(key, value);
+        editor.commit();
+        Toast.makeText(VoicemailSettingsActivity.this, "save success", Toast.LENGTH_SHORT).show();
+        finish();
+    }
+
+
     @Override
     public void onClick(View view) {
         Log.d("jasun", "=============onClick============");
(END)

If you like source code analysis series, please refer to other articles:
Android source code analysis (I) - how to quickly master Android compiled files
Android source code analysis (2) - how to compile the modified framework resource file
Android source code analysis (3) - system framework design idea
Android source code analysis (IV) - Android source code compilation and swiping steps
Android source code analysis (5) - how to design Framework framework from the perspective of architect

Topics: Android Java xml git