Android Saving Data
来源:互联网 发布:数据挖掘软件容易使用 编辑:程序博客网 时间:2024/06/07 13:01
Android Saving Data
There are three options to save data in android:
- Saving key-value pairs of simple data types in a shared preferences file
- Saving arbitrary files in Android’s file system
- Saving structured data by using SQLite database
SharedPreferences
Android framework provides the SharedPreferences APIs to storage small collection of key-values, such as login name and login pwd. Each SharedPreference file will be saved as a .xml file in the dir of “/data/data/your_app_pkg_dir”.
Access or create:
You can create a new shared preference file or access an exiting one by the sample code below(For
example, the following code is executed in a Fragment):
Context context = getActivity();SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE);
Alternativly, if you need just one shared preference file for your activity, you can just use:
SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
Write
To write to a shared preference file, create a Sharedpreferences.Editor
by calling editor()
on your sharedPreferences
, and then call the methods such as putInt()
or putString()
. At last, call commit()
to save the changes. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor editor = sharedPref.edit();editor.putString("name", "sen.tian");editor.putInt("age", 26);editor.commit();
Read
To retrieve values from a shared preferences file, call the methods such as getInt()
or getString()
, providing the key for the value you want, and optionally a default key to return if the key isn’t present. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);long highScore = sharedPref.getInt("age", 0);
Files
Internal Storage
- Always available. No need to add permission.
- Files saved here are accessible by only your app.
- When app is uninstalled, app’s files will all be removed.
External Storage
- Not Always available. Need to add permission of
android.permission.WRITE_EXTERNAL_STORAGE
in AndroidMenifest.xml. - Files saved here are world-readable, so the data will be not safe here.
- The app’s files will be removed only if you save them in the dir from
getExternalFileDir()
when app is uninstalled.
- Android Training ---Saving data
- android-Saving Data
- Android Saving Data
- saving data - Saving Files
- : saving data
- Saving Data
- Saving Data
- Saving Data
- android官方文档学习之Saving Data
- Saving Data --- Saving Files(二)
- saving data - Saving Data in SQL Databases
- Saving data to a file in your Android application
- AndroidTraining学习------Saving-Data
- Saving Data笔记
- Saving Data in iOS
- saving data - Saving Key-Value Sets
- Saving Data --- Saving Key-Value Sets(一)
- Saving Data in SQL Databases
- 字符串问题---最小包含子串的长度
- Beginning Spring学习笔记——第3章(三)文件上传、异常处理和个性化
- 单例模式
- CORS——跨域请求那些事儿
- AVL 平衡二叉搜索树原理及编程实现 (C++)版本 第二版
- Android Saving Data
- 关于内存泄漏---auto_ptr
- loadrunner11 回放脚本Action.c(94): 错误 -27979: 找不到请求的表单 [MsgId: MERR-27979]
- LR回放https协议脚本失败:[GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX" failed:[10054] Connection reset by
- android MAGNETIC_FIELD
- Hadoop 2.0 data write operation acknowledgement
- sqlcipher加密已有数据库及其时机
- 北大方正暗偷明抢,是校办企业的反面教员
- 在国内最大的手机公司工作的那些事