📜  如何查看Android Studio中共享首选项中存储的数据?

📅  最后修改于: 2021-05-10 15:41:17             🧑  作者: Mango

Android中的共享首选项是本地存储,我们可以在其中使用键和值对来保存数据。它通常用于将数据存储在用户的设备中。共享首选项还用于我们使用登录功能的Android应用程序中的会话管理。在本文中,我们将研究如何在Android Studio的“共享首选项”中查看数据存储

我们将在本文中构建什么?

我们将构建一个简单的应用程序,在该应用程序中,我们将使用简单的EditText字段将数据简单地保存在共享首选项中。之后,我们将看到此数据存储在Android Studio中的此文件中。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


  
    
    
  
    
    


Java
package com.example.gfgpagination;
  
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    // creating constant keys for shared preferences.
    public static final String SHARED_PREFS = "shared_prefs";
    // key for storing email.
    public static final String MESSAGE_KEY = "message_key";
    // variable for shared preferences.
    SharedPreferences sharedpreferences;
    // creating variables for our edit text and button. 
    private EditText messageEdt;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // getting the data which is stored in shared preferences.
        sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
        // initializing our variables. 
        messageEdt = findViewById(R.id.idEdtMessage);
        Button saveBtn = findViewById(R.id.idBtnSaveData);
        // adding on click listner for our button. 
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // inside on click we are checking if the entered data 
                // by user is empty or not. 
                String msg = messageEdt.getText().toString();
                if (TextUtils.isEmpty(msg)) {
                    // if the input is empty we are displaying a toast message.
                    Toast.makeText(MainActivity.this, "Please enter your message", Toast.LENGTH_SHORT).show();
                } else {
                    // if the input is not empty we are calling a method to save 
                    // data to shared prefs. 
                    saveMessage(msg);
                }
            }
        });
    }
  
    private void saveMessage(String msg) {
        SharedPreferences.Editor editor = sharedpreferences.edit();
        // below lines will put values for
        // message in shared preferences.
        editor.putString(MESSAGE_KEY, msg);
        // to save our data with key and value.
        editor.apply();
        // on below line we are displaying a toast message after adding data to shared prefs. 
        Toast.makeText(this, "Message saved to Shared Preferences", Toast.LENGTH_SHORT).show();
        // after that we are setting our edit text to empty
        messageEdt.setText("");
    }
}


步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

package com.example.gfgpagination;
  
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    // creating constant keys for shared preferences.
    public static final String SHARED_PREFS = "shared_prefs";
    // key for storing email.
    public static final String MESSAGE_KEY = "message_key";
    // variable for shared preferences.
    SharedPreferences sharedpreferences;
    // creating variables for our edit text and button. 
    private EditText messageEdt;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // getting the data which is stored in shared preferences.
        sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
        // initializing our variables. 
        messageEdt = findViewById(R.id.idEdtMessage);
        Button saveBtn = findViewById(R.id.idBtnSaveData);
        // adding on click listner for our button. 
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // inside on click we are checking if the entered data 
                // by user is empty or not. 
                String msg = messageEdt.getText().toString();
                if (TextUtils.isEmpty(msg)) {
                    // if the input is empty we are displaying a toast message.
                    Toast.makeText(MainActivity.this, "Please enter your message", Toast.LENGTH_SHORT).show();
                } else {
                    // if the input is not empty we are calling a method to save 
                    // data to shared prefs. 
                    saveMessage(msg);
                }
            }
        });
    }
  
    private void saveMessage(String msg) {
        SharedPreferences.Editor editor = sharedpreferences.edit();
        // below lines will put values for
        // message in shared preferences.
        editor.putString(MESSAGE_KEY, msg);
        // to save our data with key and value.
        editor.apply();
        // on below line we are displaying a toast message after adding data to shared prefs. 
        Toast.makeText(this, "Message saved to Shared Preferences", Toast.LENGTH_SHORT).show();
        // after that we are setting our edit text to empty
        messageEdt.setText("");
    }
}

现在运行您的应用程序,并在编辑文本字段中添加一些数据,然后单击按钮以将数据保存到共享首选项。您可以在下面的视频中查看该过程:

输出:

步骤4:查看共享首选项中存储的数据

现在在右下角的Android Studio中,您将看到该选项作为Device File Explorer 。单击该选项,您将看到以下屏幕。在此屏幕内,导航到data> data folder

步骤5:打开包含共享首选项的文件

现在,在其中检查您的应用程序的软件包名称。单击您的应用程序的软件包名称。之后,单击shared_prefs文件夹,然后在其中打开shared_prefs.xml文件。现在,您将从应用程序中查看存储在共享首选项中的数据。