西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 7 Data Persistence

Android Programming Lecture 7 Data Persistence
Android Programming Lecture 7 Data Persistence

What is Persistence? Persistence is "the continuance of an effect after its cause is removed".In the context of storing data in a computer system,this means that the data survives after the process with which it was created has ended.In other words,for a data store to be considered persistent,it must write to non- volatile storage. 2
What is Persistence? Persistence is “the continuance of an effect after its cause is removed”. In the context of storing data in a computer system, this means that the data survives after the process with which it was created has ended. In other words, for a data store to be considered persistent, it must write to nonvolatile storage. 2

Saving Data in Android App data is private to the application Internal Storage:Store data on the device memory temporarily Shared Preferences:Lightweight mechanism to store private primitive data in key-value pairs in hard drive File:Open and save files on the device or removable storage SQLite Database:Store structured data in a private database Network Connection:Store data on the web Content provider is used to give the data to other apps
Saving Data in Android • App data is private to the application • Internal Storage: Store data on the device memory temporarily • Shared Preferences: Lightweight mechanism to store private primitive data in key-value pairs in hard drive • File: Open and save files on the device or removable storage • SQLite Database: Store structured data in a private database • Network Connection: Store data on the web • Content provider is used to give the data to other apps 3

Passing Temporal Data using Intent Intent intent new Intent(Activity1.this,Activity2.class); Activity 1 Bundle data new Bundle(); data.putstring("key_name","name"); data.putstring("key_age","age"); intent.putExtras(data); Intent intent.putExtra("key_id","id"); intent.putExtra("key_address","address"); startActivity(intent); /In Activity2.java Activity 2 /Retrieve the intent Intent intent getIntent(); /Retrieve the string data in the intent String name intent.getstringExtra("key_name"); String age intent.getstringExtra("key_age"); String id intent.getstringExtra("key_id"); String address intent.getstringExtra("key_address"); 4
4 Activity 1 Activity 2 Intent Intent intent = new Intent(Activity1.this, Activity2.class); Bundle data = new Bundle(); data.putString("key_name", "name"); data.putString("key_age", "age"); intent.putExtras(data); intent.putExtra("key_id", "id"); intent.putExtra("key_address", "address"); startActivity(intent); // In Activity2.java // Retrieve the intent Intent intent = getIntent(); // Retrieve the string data in the intent String name = intent.getStringExtra("key_name"); String age = intent.getStringExtra("key_age"); String id = intent.getStringExtra("key_id"); String address = intent.getStringExtra("key_address"); Passing Temporal Data using Intent

Data Persistence in Orientation When screen rotation is changed,the activity is destroyed and opened again How to store state information Store state:-onSaveInstanceState(Bundle) Read state:-onRestoreInstancestate(Bundle) This will store data only temporarily for app lifetime! o Data will be held in memory until the app is closed! Refer to the Android Developer Site: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Data Persistence in Orientation • When screen rotation is changed, the activity is destroyed and opened again • How to store state information • Store state: – onSaveInstanceState(Bundle) • Read state: – onRestoreInstanceState(Bundle) • This will store data only temporarily for app lifetime! o Data will be held in memory until the app is closed! 5 Refer to the Android Developer Site: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Saving Data in Android ·Internal Storage Shared Preferences ·File ·SQLite Database ·Network Connection 6
Saving Data in Android • Internal Storage • Shared Preferences • File • SQLite Database • Network Connection 6

Shared Preferences The SharedPreferences interface (in package android.content)provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types and strings. o similar to saving data in a Bundle Can be used to save the following data types o Boolean float o int long o String Set Shared preference data will persist across user sessions even if the application is killed
Shared Preferences • The SharedPreferences interface (in package android.content) provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types and strings. o similar to saving data in a Bundle • Can be used to save the following data types o Boolean – float o int – long o String − Set • Shared preference data will persist across user sessions even if the application is killed. 7

public class Example extends Activity /Create a preference file with the name specified public static final String PREFS_NAME "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); /Restore the preferences //Get the preference with the name specified SharedPreferences settings getsharedPreferences(PREFS_NAME,0); /Read the stored data from the preference int data settings.getInt("key",defaultvalue); com.example.toml.sharedpreference 2015-09-0212:12dw0-x-x cache 2015-09-02 11:23 drwxrwx--x @Override 三ib 2015-09-0211:23 Irwxnwxrwx >/data/a... protected void onStop(){ 2015-09-02 1212 drwxrwx--x 目Filel.xml 1092015-09-0212:12 super.onstop(); -nw-n---- /Store the preferences /We need an Editor object to make preference changes. SharedPreferences settings getsharedPreferences(PREFS_NAME,0); SharedPreferences.Editor editor settings.edit(); editor.putInt("key",value); //Commit the edits! editor.commit(); 8
8 public class Example extends Activity { // Create a preference file with the name specified public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore the preferences // Get the preference with the name specified SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Read the stored data from the preference int data = settings.getInt(“key", defaultValue); . . . } @Override protected void onStop(){ super.onStop(); // Store the preferences // We need an Editor object to make preference changes. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt(“key”, value); // Commit the edits! editor.commit(); } }

Overview of Shared Preferences Examples of data stored in shared preferences: o user name -password o email address high score An application can have multiple sets of application preferences,where each set has a name Preferences can be stored at the activity level or the application level.In general,they are not shared outside the application. Application preferences are stored in XML files in the Android file system as follows: /data/data//shared_prefs/.xml
Overview of Shared Preferences • Examples of data stored in shared preferences: o user name – password o email address – high score • An application can have multiple sets of application preferences, where each set has a name. • Preferences can be stored at the activity level or the application level. In general, they are not shared outside the application. • Application preferences are stored in XML files in the Android file system as follows: /data/data//shared_prefs/.xml 9

Obtaining a SharedPreferences Object Two methods that return a SharedPreferences object: getSharedPreferences(String name,int mode) o Use if you need multiple preferences files identified by name. o Name is specified as the first parameter. o If a preferences file by this name does not exist,it will be created when you retrieve an editor. o Preferences can be accessed by all activities in the application. getPreferences(int mode) o Use if you need only one preferences file for your Activity. o Only one preferences file for an Activity-don't supply a name. o Calls method getsharedPreferences(String,int)passing in this activity's class name as the preferences name. o Preferences are not shared with other activities in the application. 10
Obtaining a SharedPreferences Object Two methods that return a SharedPreferences object: • getSharedPreferences(String name, int mode) o Use if you need multiple preferences files identified by name. o Name is specified as the first parameter. o If a preferences file by this name does not exist, it will be created when you retrieve an editor. o Preferences can be accessed by all activities in the application. • getPreferences(int mode) o Use if you need only one preferences file for your Activity. o Only one preferences file for an Activity – don't supply a name. o Calls method getSharedPreferences(String, int) passing in this activity’s class name as the preferences name. o Preferences are not shared with other activities in the application. 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 6 List View and Custom View.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 5 Intent.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 4 Activity, Intent and UI.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 3 File structure and Layout.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 2 Introduction to Java and Object Oriented Programming.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 1 Introduction to Network Computing(主讲:栾浩).pptx
- 《算法基础》课程教学资源(学习笔记)算法基础 课堂笔记.pdf
- 长沙理工大学:《微机原理与接口技术》课程教学资源(大纲教案)微机原理与应用授课教案(负责人:叶青,打印版).pdf
- 同济大学:《逻辑网络》课程电子教案(PPT课件)数字设计中的基本电路 Introduction to the circuits in digital design.ppt
- 同济大学:《逻辑网络》课程电子教案(PPT课件)异步时序电路分析与设计 Introduction to asynchronous circuits design.ppt
- 同济大学:《逻辑网络》课程电子教案(PPT课件)寄存器与计数器 register and counters.ppt
- 同济大学:《逻辑网络》课程电子教案(PPT课件)同步时序电路设计中的问题 Advanced design issue.ppt
- 同济大学:《逻辑网络》课程教学资源(试卷习题)考试样卷.doc
- 同济大学:《逻辑网络》课程教学资源(教学大纲)逻辑网络(英文)Logic networks.doc
- 同济大学:《逻辑网络》课程教学资源(教学大纲)逻辑网络(中文,负责人:周俊鹤).doc
- 北京化工大学:《数据结构》课程PPT教学课件(C语言描述)第六章 查找.ppt
- 北京化工大学:《数据结构》课程PPT教学课件(C语言描述)第五章 图.ppt
- 北京化工大学:《数据结构》课程PPT教学课件(C语言描述)第三章 栈和队列.ppt
- 北京化工大学:《数据结构》课程PPT教学课件(C语言描述)第二章 线性表.ppt
- 北京化工大学:《数据结构》课程PPT教学课件(C语言描述)第一章 绪论(负责人:侯虹).ppt
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 8 Multi-threading.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 9 Service and Broadcast Receiver.pptx
- 西安电子科技大学:《网络计算》课程PPT教学课件(Android Programming)Lecture 10 Multimedia.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 01 Soft Testing - Fundamentals of Testing.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 02 Testing throughout the Software Lifecycle.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 03 Static Techniques.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 04 Test Design Techniques.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 05 Test Management.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)Chapter 06 Tool Support for Testing.pptx
- 同济大学:《软件测试》课程电子教案(PPT课件)How To Do High Quality Research, Write Acceptable Papers, and Make Effective Presentations?.ppt
- 《软件测试》课程电子教案(参考资料)Standard glossary of terms used in Software Testing(Version 2.0).pdf
- 《软件测试》课程电子教案(参考资料)Certified Tester Foundation Level Syllabus Released(Version 2011).pdf
- 《软件测试》课程电子教案(参考资料)Certified Tester Foundation Level Syllabus Released(Version 2011).pdf
- 河南科技大学:信息工程学院教育技术学专业本科课程教学大纲(汇编).pdf
- 吉林大学:《人工智能》课程电子教案(PPT课件)第一章 绪论 Artificial Intelligence(AI).ppt
- 吉林大学:《人工智能》课程电子教案(PPT课件)第七章 机器学习.ppt
- 吉林大学:《人工智能》课程电子教案(PPT课件)第三章 知识与知识表示.ppt
- 吉林大学:《人工智能》课程电子教案(PPT课件)第二章 人工智能的数学基础.ppt
- 吉林大学:《人工智能》课程电子教案(PPT课件)第五章 搜索策略.ppt
- 吉林大学:《人工智能》课程电子教案(PPT课件)第八章 智能决策支持系统.ppt