Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷(xiāo)功能
Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷(xiāo)功能
前言
本文用SharedPreferences本地緩存賬號(hào)信息來(lái)實(shí)現(xiàn)登錄注冊(cè)功能,以及退出注銷(xiāo)功能。
一、本文邏輯
本文的注冊(cè)登錄邏輯如下:
1、注冊(cè)頁(yè)面:有賬號(hào)可以直接去登錄頁(yè)面。沒(méi)有賬號(hào)的話(huà)填寫(xiě)賬號(hào)密碼,檢測(cè)賬號(hào)密碼不為空,點(diǎn)擊立即注冊(cè),保存賬號(hào)信息,跳轉(zhuǎn)到登錄頁(yè)面。
2、登錄頁(yè)面:首先讀取緩存的賬號(hào)密碼和是否記住密碼,將賬號(hào)顯示,判斷記住密碼的標(biāo)志,為空或false,不顯示密碼,需要輸入密碼,為true則直接將緩存的密碼顯示,選擇是否記住密碼按鈕,將此狀態(tài)存入緩存,點(diǎn)擊登錄跳轉(zhuǎn)到主頁(yè)。
3、主頁(yè):首先取緩存,判斷是否登錄,若沒(méi)有登錄跳轉(zhuǎn)到注冊(cè)頁(yè)面。點(diǎn)擊退出登錄,跳轉(zhuǎn)到注冊(cè)頁(yè);(2)點(diǎn)擊注銷(xiāo)賬號(hào),清除所有緩存,跳轉(zhuǎn)到注冊(cè)頁(yè)面。
二、代碼
1.布局界面
注冊(cè)頁(yè)面 activity_register.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:background="#AB2196F3" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:text="用戶(hù)注冊(cè)" ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? android:textStyle="bold" /> ? ? </RelativeLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? android:layout_marginTop="20dp" ? ? ? ? android:orientation="vertical"> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/reg_uname" ? ? ? ? ? ? android:layout_marginTop="40dp" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:hint="帳號(hào)"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/reg_pwd" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:inputType="textPassword" ? ? ? ? ? ? android:hint="密碼"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/reg_pwd2" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:inputType="textPassword" ? ? ? ? ? ? android:hint="再次確認(rèn)密碼"/> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/register_btn" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:background="#AB2196F3" ? ? ? ? ? ? android:text="立即注冊(cè)"/> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/register_toLogin_btn" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:background="#AB2196F3" ? ? ? ? ? ? android:text="已有賬號(hào)?去登錄"/> ? ? </LinearLayout> </LinearLayout>
登錄界面 activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:gravity="center_horizontal" ? ? android:orientation="vertical"> ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? android:background="#AB2196F3" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:text="用戶(hù)登錄" ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? android:textStyle="bold" /> ? ? </RelativeLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:layout_marginTop="20dp" ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? android:orientation="vertical"> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/login_username" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:hint="賬號(hào)"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/login_password" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:hint="密碼" ? ? ? ? ? ? android:inputType="textPassword" ? ? ? ? ? ? android:layout_margin="10dp"/> ? ? ? ? <RelativeLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_margin="10dp"> ? ? ? ? ? ? <CheckBox ? ? ? ? ? ? ? ? android:id="@+id/login_remember" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="記住密碼" /> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? ? ? ? ? android:layout_centerVertical="true" ? ? ? ? ? ? ? ? android:clickable="true" ? ? ? ? ? ? ? ? android:onClick="register" ? ? ? ? ? ? ? ? android:textColor="#3F51B5" ? ? ? ? ? ? ? ? android:text="沒(méi)有賬號(hào)?立即注冊(cè)" /> ? ? ? ? </RelativeLayout> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/login_btn" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:background="#AB2196F3" ? ? ? ? ? ? android:text="登錄" ? ? ? ? ? ? android:layout_margin="10dp"/> ? ? </LinearLayout> </LinearLayout>
主頁(yè)面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? android:padding="15dp" ? ? tools:context=".MainActivity"> ? ? <TextView ? ? ? ? android:id="@+id/text" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="主頁(yè)!" ? ? ? ? android:textSize="30dp" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ?android:layout_alignParentTop="true"/> ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="主頁(yè)內(nèi)容!" ? ? ? ? android:textSize="30dp" ? ? ? ? android:layout_marginTop="50dp" ? ? ? ? android:layout_below="@id/text" ? ? ? ? android:layout_centerHorizontal="true" /> ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="bottom" ? ? ? ? android:text="退出登錄" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:onClick="Quit"/> ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="bottom" ? ? ? ? android:text="注銷(xiāo)此賬號(hào)" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? android:onClick="zhuxiao"/> </RelativeLayout>
2.Activity 邏輯
注冊(cè)頁(yè)面的邏輯
package com.example.yuan; import android.content.Intent; 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.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; /** ?* Created by Yuan on 2020/8/6 17:08. ?* 包名: com.example.yuan ?* 類(lèi)說(shuō)明:注冊(cè)(按鈕的點(diǎn)擊事件直接使用 Activity 作為事件監(jiān)聽(tīng)器) ?*/ public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { ? ? //獲取用戶(hù)名,昵稱(chēng),密碼,確認(rèn)密碼 ? ? private EditText reg_uname,reg_uname2,et_pwd,et_pwd2; ? ? //獲取用戶(hù)名,昵稱(chēng),密碼,確認(rèn)密碼的值 ? ? private String uname,pwd,pwd2; ? ? private SharedPreferences sp; ? ? private SharedPreferences.Editor editor; private Button btn_reg,btn_toLogin; ? ? @Override ? ? protected void onCreate(@Nullable Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_register); ? ? ? ? init();//獲取界面控件 ? ? } ? ? //UI組件初始化與事件綁定 ? ? private void init(){ ? ? ? ? //UI組件初始化 ? ? ? ? reg_uname=findViewById(R.id.reg_uname); ? ? ? ? et_pwd=findViewById(R.id.reg_pwd); ? ? ? ? et_pwd2=findViewById(R.id.reg_pwd2); ? ? ? ? btn_toLogin=findViewById(R.id.register_toLogin_btn); ? ? ? ? btn_reg=findViewById(R.id.register_btn); ? ? ? ? //點(diǎn)擊事件綁定 ? ? ? ? btn_reg.setOnClickListener(this); ? ? ? ? btn_toLogin.setOnClickListener(this); ? ? } //點(diǎn)擊事件 ? ? @Override ? ? public void onClick(View v) { ? ? ? ? switch (v.getId()){ ? ? ? ? ? ? case R.id.register_btn: ? ? ? ? ? ? ? ? //獲取參數(shù) ? ? ? ? ? ? ? ? uname=reg_uname.getText().toString().trim(); ? ? ? ? ? ? ? ? pwd=et_pwd.getText().toString().trim(); ? ? ? ? ? ? ? ? pwd2=et_pwd2.getText().toString().trim(); ? ? ? ? ? ? ? ? //判斷 ? ? ? ? ? ? ? ? if(uname.equals("") || pwd.equals("") ){ ? ? ? ? ? ? ? ? ? ? Toast.makeText(RegisterActivity.this, "用戶(hù)名或密碼不能為空", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? }else if(!(pwd.equals(pwd2))){ ? ? ? ? ? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"兩次輸入密碼不一致",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? //loginInfo表示文件名 ? ? ? ? ? ? ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? ? ? ? ? ? ? //獲取編輯器 ? ? ? ? ? ? ? ? ? ? editor=sp.edit(); ? ? ? ? ? ? ? ? ? ? //存入注冊(cè)的用戶(hù)信息 ? ? ? ? ? ? ? ? ? ? editor.putString("username", uname); ? ? ? ? ? ? ? ? ? ? editor.putString("password",pwd); ? ? ? ? ? ? ? ? ? ? //提交修改 ? ? ? ? ? ? ? ? ? ? editor.commit(); ? ? ? ? ? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"注冊(cè)成功",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? startActivity(new Intent(getApplicationContext(),LoginActivity.class)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.register_toLogin_btn://跳轉(zhuǎn)到登錄頁(yè)面 ? ? ? ? ? ? ? ? startActivity(new Intent(getApplicationContext(),LoginActivity.class)); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } }
登錄頁(yè)面的邏輯
package com.example.yuan; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; /** ?* Created by Yuan on 2020/8/6 16:12. ?* 包名: com.example.yuan ?* 類(lèi)說(shuō)明:登錄 ?*/ public class LoginActivity extends AppCompatActivity { ? ? private EditText et_uname,et_upwd;//獲取用戶(hù)名,密碼 ? ? private CheckBox isRemember; ? ? private Button login_btn; ? ? private String username,password,sp_name,sp_pwd; ? ? private Boolean sp_isRemember;//是否記住密碼的標(biāo)志 ? ? private Context mContext; ? ? private SharedPreferences sp; ? ? private SharedPreferences.Editor editor; ? ? @Override ? ? protected void onCreate(@Nullable Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_login); ? ? ? ? init();//獲取界面控件 ? ? ? ? //讀取緩存數(shù)據(jù) ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? sp_name=sp.getString("username",""); ? ? ? ? sp_pwd=sp.getString("password",""); ? ? ? ? sp_isRemember=sp.getBoolean("isRemember",false); ? ? ? ? //如果有賬號(hào),直接顯示 ? ? ? ? Log.d("TAG",sp_name); ? ? ? ? et_uname.setText(sp_name); ? ? ? ? //如果上次登錄記住了密碼,本次登錄直接設(shè)置上 ? ? ? ? if(sp_isRemember.equals(true)){ ? ? ? ? ? ? et_upwd.setText(sp_pwd); ? ? ? ? ? ? isRemember.setChecked(true); ? ? ? ? } ? ? ? ? mContext= LoginActivity.this; ? ? ? ? //登錄按鈕的點(diǎn)擊事件(直接用匿名內(nèi)部類(lèi)) ? ? ? ? login_btn.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? //開(kāi)始登錄,獲取輸入的用戶(hù)名和密碼 ? ? ? ? ? ? ? ?username = et_uname.getText().toString().trim(); ? ? ? ? ? ? ? ?password = et_upwd.getText().toString().trim(); ? ? ? ? ? ? ? ? // TextUtils.isEmpty判斷是否輸入 ? ? ? ? ? ? ? ? if (TextUtils.isEmpty(username)) { ? ? ? ? ? ? ? ? ? ? Toast.makeText(mContext, "請(qǐng)輸入手機(jī)號(hào)", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } else if (TextUtils.isEmpty(password)) { ? ? ? ? ? ? ? ? ? ? Toast.makeText(mContext, "請(qǐng)輸入密碼", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? // 判斷,輸入的賬號(hào)密碼,是否與保存在SharedPreferences中一致 ? ? ? ? ? ? ? ? } else if (username.equals(sp_name) && password.equals(sp_pwd)) { ? ? ? ? ? ? ? ? ? ? //一致登錄成功 ? ? ? ? ? ? ? ? ? ? Toast.makeText(mContext, "登錄成功", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? //判斷記住密碼按鈕是否選中,選中則將isRemember設(shè)置為true,保存狀態(tài) ? ? ? ? ? ? ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? ? ? ? ? ? ? //獲取編輯器 ? ? ? ? ? ? ? ? ? ? editor=sp.edit(); ? ? ? ? ? ? ? ? ? ? if (isRemember.isChecked()) {//如果記住密碼選中,存入true,否則存入false ? ? ? ? ? ? ? ? ? ? ? ? editor.putBoolean("isRemember",true); ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? editor.putBoolean("isRemember",false); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? editor.putBoolean("isLogin",true);//保存登錄狀態(tài) ? ? ? ? ? ? ? ? ? ? editor.apply();//提交修改 ? ? ? ? ? ? ? ? ? ? //登錄成功后關(guān)閉此頁(yè)面進(jìn)入主頁(yè) ? ? ? ? ? ? ? ? ? ? //跳轉(zhuǎn)到主界面,登錄成功的狀態(tài)傳遞到 MainActivity 中 ? ? ? ? ? ? ? ? ? ? Intent intent=new Intent(getApplicationContext(),MainActivity.class); ? ? ? ? ? ? ? ? ? ? intent.putExtra("name",username); ? ? ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? ? ? ? ? finish();//銷(xiāo)毀登錄界面 ? ? ? ? ? ? ? ? } else if ((sp_pwd != null && !TextUtils.isEmpty(sp_pwd) && !password.equals(sp_pwd))) { ? ? ? ? ? ? ? ? ? ? Toast.makeText(mContext, "輸入的用戶(hù)名和密碼不一致", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? Toast.makeText(mContext, "此用戶(hù)名不存在", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? //獲取界面控件 ? ? private void init(){ ? ? ? ? et_uname=findViewById(R.id.login_username); ? ? ? ? et_upwd=findViewById(R.id.login_password); ? ? ? ? isRemember=findViewById(R.id.login_remember); ? ? ? ? login_btn=findViewById(R.id.login_btn); ? ? } //注冊(cè)按鈕的點(diǎn)擊事件(直接綁定在控件上) ? ? public void register(View view) { ? ? ? ? Intent intent=new Intent(getApplicationContext(),RegisterActivity.class); ? ? ? ? startActivity(intent); ? ? } }
主頁(yè)面的邏輯
package com.example.yuan; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ? ? private ?SharedPreferences sp; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? TextView textView=findViewById(R.id.text); ? ? ? ? Intent intent=getIntent(); ? ? ? ? String name=intent.getStringExtra("name"); ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? textView.setText("歡迎你,"+name); ? ? ? ? Boolean isLogin=sp.getBoolean("isLogin",false); ? ? ? ? if (!isLogin){ ? ? ? ? ? ? startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); ? ? ? ? ? ? finish(); ? ? ? ? }else if(TextUtils.isEmpty(name)){ ? ? ? ? ? ? startActivity(new Intent(getApplicationContext(),LoginActivity.class)); ? ? ? ? ? ? finish(); ? ? ? ? } ? ? } ? ? public void Quit(View view) { ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? SharedPreferences.Editor editor=sp.edit(); ? ? ? ? editor.putBoolean("isLogin",false); ? ? ? ? editor.commit(); ? ? ? ? startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); ? ? ? ? finish(); ? ? } ? ? /** ? ? ?* @param view ? ? ?*/ ? ? public void zhuxiao(View view) { ? ? ? ? sp=getSharedPreferences("loginInfo", MODE_PRIVATE); ? ? ? ? SharedPreferences.Editor editor=sp.edit(); ? ? ? ? editor.clear(); ? ? ? ? editor.commit(); ? ? ? ? Toast.makeText(MainActivity.this,"注銷(xiāo)成功",Toast.LENGTH_SHORT).show(); ? ? ? ? startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); ? ? ? ? finish(); ? ? } }
總結(jié)
在android 中存儲(chǔ)數(shù)據(jù)時(shí)經(jīng)常用SharedPreference, 并且在提交數(shù)據(jù)時(shí)一直用的是Editor的commit方法, 也有用apply方法的。
apply和commit方法的不同:
apply沒(méi)有返回值而commit返回boolean表明修改是否提交成功
apply是將修改數(shù)據(jù)原子提交到內(nèi)存, 而后異步真正提交到硬件磁盤(pán), 而commit是同步的提交到硬件磁盤(pán),因此,在多個(gè)并發(fā)的提交commit的時(shí)候,他們會(huì)等待正在處理的commit保存到磁盤(pán)后在操作,從而降低了效率。而apply只是原子的提交到內(nèi)容,后面有調(diào)用apply的函數(shù)的將會(huì)直接覆蓋前面的內(nèi)存數(shù)據(jù),這樣從一定程度上提高了很多效率。
apply方法不會(huì)提示任何失敗的提示。
由于在一個(gè)進(jìn)程中,sharedPreference是單實(shí)例,一般不會(huì)出現(xiàn)并發(fā)沖突,如果對(duì)提交的結(jié)果不關(guān)心的話(huà),建議使用apply,當(dāng)然需要確保提交成功且有后續(xù)操作的話(huà),還是需要用commit的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)例代碼,可以應(yīng)用于Android游戲開(kāi)發(fā)中的一個(gè)應(yīng)用,需要的朋友可以參考下2014-07-07Android編程實(shí)現(xiàn)通過(guò)反射獲取資源Id的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)通過(guò)反射獲取資源Id的方法,結(jié)合實(shí)例形式分析了Android反射機(jī)制操作資源的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01android新建草稿刪除后下次開(kāi)機(jī)還會(huì)顯示保存的草稿
android 新建一個(gè)草稿,保存,然后全部刪除會(huì)話(huà),關(guān)機(jī)再開(kāi)機(jī)后還會(huì)顯示保存的草稿,下面與大家分享下具體的解決方法2013-06-06Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03如何利用Android Studio將moudle變成jar示例詳解
這篇文章主要給大家介紹了關(guān)于如何利用Android Studio將moudle變成jar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn)
Android 多屏互聯(lián)的時(shí)代,必然會(huì)出現(xiàn)多屏連接的問(wèn)題,本文主要介紹了Android?雙屏異顯自適應(yīng)Dialog的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android?hid發(fā)送apdu格式數(shù)據(jù)示例詳解
這篇文章主要介紹了Android?hid發(fā)送apdu格式數(shù)據(jù),在?Android?中,如果你想通過(guò)?HID(Human?Interface?Device)發(fā)送?APDU?格式的數(shù)據(jù),通常會(huì)涉及?USB?HID?設(shè)備或藍(lán)牙?HID?設(shè)備,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-08-08