irpas技术客

Android SharedPreferences 详解及activity之间如何传递数据、开关控制_ys.journey

irpas 3479

Android SharedPreferences是什么

Sharedpreferences 是Android平台上的一个轻量级的存储类,可以用来保存应用程序的一些配置信息,它的本质是 "键值对"的方式进行保存数据,同时也可以作为数据传递,开关控制,等一些功能。它的文件保存在 /data/data//shared_prefs 目录下,它的优点是 不会产生 空指针、Application、静态变量,但是缺点就是效率不高。 Android 五种数据存储的方式分别为:

文件存储:以IO流形式存放,可分为手机内部和手机外部(sd卡等)存储,可存放较大数据;SQLite:轻量级、跨平台数据库,将所有数据都是存放在手机上的单一文件内,占用内存小;SharedPreferences:以Map形式存放简单的配置参数;ContentProvider:将应用的私有数据提供给其他应用使用;网络存储 :数据存储在服务器上,通过连接网络获取数据;

今天我们着重介绍 SP的使用:

Android SharedPreferences怎么用

数据存储

1、写数据 //步骤1:创建一个SharedPreferences及editor private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; //步骤2:实例化对象 sharedPreferences = getSharedPreferences("data_storage", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); //步骤3:将要存储的值放入文件--此处列举各种类型的存储 editor.putString("name","fancy"); editor.putInt("age","21"); editor.putBoolean("bool",false); //步骤4:提交 editor.commit(); 2、读数据 private String name; private int age; private boolean status; name = sharedPreferences.getString("name",""); age = sharedPreferences.getInt("age",1); status = sharedPreferences.getBoolean("bool",false); 3、删除指定数据 editor.remove("name"); editor.commit(); 4、清空数据 editor.clear(); editor.commit();

需要注意的地方:getSharedPreferences()方法 此方法接收两个参数,第一个参数用于指定 SharedPreferences 文件的名称,如果指定的文件不存在则会创建一个,第二个参数用于指定操作模式,主要有以下几种模式可以选择。MODE_PRIVATE 是默认的操作模式,和直接传入 0 效果是相同的。 MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE 这两种模式已在 Android 4.2 版本中被废弃。

Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写 Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写 Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读 Context.MODE_APPEND:该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件

数据传递 sp也可以在两个activity之间进行数据传递,或者当做开关控制,上面已经讲解了sp的具体用法,下面我们直接示例: MainActivity

import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private Button btn_save; private EditText editText; private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_save = (Button) findViewById(R.id.btn_save); editText = (EditText) findViewById(R.id.display_save); btn_save.setOnClickListener(mOnClickListener); sharedPreferences = getSharedPreferences("communication", Context.MODE_PRIVATE); editor = sharedPreferences.edit(); } private final View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View view) { int id = view.getId(); switch (id) { case R.id.btn_save: editor.putString("password",editText.getText().toString()); editor.commit(); Intent intent = new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); break; } } }; }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_save" android:layout_width="150dp" android:layout_height="wrap_content" android:textSize="20dp" android:gravity="center" android:text="保存的数据:" /> <EditText android:id="@+id/display_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="5dp" android:paddingTop="5dp" android:paddingEnd="10dp" android:paddingBottom="5dp" android:singleLine="true" /> </LinearLayout> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存"/> </LinearLayout>

MainActivity2

import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; public class MainActivity2 extends AppCompatActivity { private SharedPreferences sp_receiver; private SharedPreferences.Editor editor; private TextView textView; private String string; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); textView = (TextView) findViewById(R.id.tv_display); sp_receiver = getSharedPreferences("communication", Context.MODE_PRIVATE); editor = sp_receiver.edit(); string = sp_receiver.getString("password","s"); textView.setText(string+" "); } }

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity2"> <TextView android:id="@+id/tv_display" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20dp"/> </LinearLayout>

看一下效果 界面一: 界面二: SP在平常不仅可以作为数据存储,还可以在 不同的activity之间当做开关来使用,同时也可以在activity之间进行数据传递。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android