Android实现手机内存存储功能
生活随笔
收集整理的這篇文章主要介紹了
Android实现手机内存存储功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android實現手機內存存儲功能
- 效果圖
- 寫入數據
- 讀取數據
- 源碼
- AddDataTestActivity
- activity_add_data_test.xml
- SharedPreferencesUtil
- Log打印日志類(LogUtil)
- ---------------------------------
- SharedPreferences.Editor的commit()方法和apply()方法的區別
- ---------------------------------
- 聯系阿超
- (可解疑解答)
- 阿超QQ:2425991616
- 阿超微信:2425991616
- 阿超郵箱:2425991616@qq.com || aboutgaojc@163.com
效果圖
點擊存入,點擊獲取 吐司結果
寫入數據
讀取數據
源碼
AddDataTestActivity
package com.gaojc.activity;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.View; import android.widget.Toast;import com.gaojc.text.R; import com.gaojc.text.Utils.LogUtil; import com.gaojc.text.Utils.SharedPreferencesUtil;public class AddDataTestActivity extends AppCompatActivity {// 需要存入的數據(這里為了方便,所以使用假數據)private final static String CODE = "ABC666";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_data_test);/*** 存入數據*/findViewById(R.id.saveData).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {SharedPreferencesUtil.getInstance(AddDataTestActivity.this).putString("code",CODE);}});/*** 獲取數據*/findViewById(R.id.getData).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String code = SharedPreferencesUtil.getInstance(AddDataTestActivity.this).getString("code");Toast.makeText(AddDataTestActivity.this,"code:" + code,Toast.LENGTH_LONG).show();LogUtil.e("code:" + code);}});} }activity_add_data_test.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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="com.gaojc.activity.AddDataTestActivity"><Buttonandroid:id="@+id/saveData"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="存入"android:layout_gravity="left"/><Buttonandroid:id="@+id/getData"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獲取"android:layout_gravity="right"/></FrameLayout>SharedPreferencesUtil
package com.gaojc.text.Utils;import android.content.Context; import android.content.SharedPreferences; import android.text.TextUtils;public class SharedPreferencesUtil {private static SharedPreferences mSharedPreferences; //保存數據接口private static SharedPreferences.Editor mEditor; //Editor對象用來寫入數據private static SharedPreferencesUtil mSharedPreferencesUtil;// 構造方法public SharedPreferencesUtil(Context context) {/*** context.getSharedPreferences:獲取SharedPreferences實例* Context.MODE_PRIVATE:指定該SharedPreferences數據只能被本應用程序讀寫*/mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);// 獲取Editor對象mEditor = mSharedPreferences.edit();}// 單例模式public static SharedPreferencesUtil getInstance(Context context) {if (mSharedPreferencesUtil == null) {mSharedPreferencesUtil = new SharedPreferencesUtil(context);}return mSharedPreferencesUtil;}// 存入數據public void putString(String key, String value) {if (TextUtils.isEmpty(value)) {value = "";}// apply():提交mEditor.putString(key, value).apply();}// 獲取數據public String getString(String key) {return mSharedPreferences.getString(key, "");} }Log打印日志類(LogUtil)
---------------------------------
SharedPreferences.Editor的commit()方法和apply()方法的區別
Android存儲(SharedPreferences.Editor)commit()和apply()區別:
commit和apply雖然都是原子性操作,但是原子的操作不同,commit是原子提交到數據庫,從提交數據到存在磁盤中都是同步過程;
而apply方法的原子操作是原子提交到內存中,而非數據庫,之后再異步提交數據到數據庫中。
commit提交是同步過程,效率會比apply異步提交的速度慢,有返回值;apply沒有返回值,無法知道存儲是否失敗。
在不關心提交結果是否成功的情況下,優先考慮apply方法。
---------------------------------
聯系阿超
(可解疑解答)
阿超QQ:2425991616
阿超微信:2425991616
阿超郵箱:2425991616@qq.com || aboutgaojc@163.com
總結
以上是生活随笔為你收集整理的Android实现手机内存存储功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜索引擎Autonomy
- 下一篇: 戴尔计算机显卡型号,戴尔笔记本电脑型号有