西安电子科技大学:《Mobile Programming》课程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
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 Intentintent =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. putExtra(key id","id"); Intent intent. putExtra(key address","address") startActivity (intent); / In Activity2. java Activity 2 // Retrieve the intent Intent intent getIntento Retrieve the string data in the intent String name intent getstringExtra(" key name"; String age intent getstringExtra("key age"); String id= intent. getstring Extra("key id); String address intent getstringExtra( key address");
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 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.androidcom/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
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 t Create a preference file with the name specified public static final String PREFS NAME "MyPrefs file"i @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)j / Read the stored data from the preference int data ttings getInt(key", defaultvalue; 201509-0212:12dnwx-x--x 2015-09-0211:23 drwxrwx--x @Override b cache 2015-09-02 11: 23 Irwxrwxrwx ->/data/ protected void onStop(t 2015-09-021212 drwxrwx-Xx D Filelxml super. onstop o; 109201509-021212-rwrw- // Store the preferences // We need an Editor object to make preference changes SharedPreferences settings= getsharedPreferences(PreFs NAME, 0) SharedPreferences Editor editor settings edito 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 9
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 get SharedPreferences(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课件讲稿)第四章 串.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)第1章 数据库技术概论.ppt
- Urandaline Investments The Perils of Down Under:Chinese Investment in Australia.pptx
- 《计算机网络》课程教学资源(PPT课件讲稿)第六章 IP路由.ppt
- 《微型计算机原理及应用》课程教学资源(PPT课件讲稿)第2章 微处理器.ppt
- Landmark-Based Speech Recognition.ppt
- 中国科学技术大学:《现代密码学理论与实践》课程教学资源(PPT课件讲稿)第9章 公钥密码学与RSA.pptx
- 中国科学技术大学:《数据结构及其算法》课程电子教案(PPT课件讲稿)第六章 二叉树和树.pps
- 计算机外设及电源故障处理(PPT课件讲稿).ppt
- 《计算机系统结构》课程教学资源(PPT课件讲稿)第三章 流水线技术.ppt
- 四川大学:《Java面向对象编程》课程PPT教学课件(Object-Oriented Programming - Java)Unit 1.2 Designing Classes.ppt
- 软件开发环境与工具的选用(PPT课件讲稿)Select software development tool.ppt
- 电子科技大学:《微机原理与接口技术》课程教学资源(PPT实验讲稿,习友宝).ppt
- 北京师范大学:《多媒体技术与网页制作》课程教学资源(PPT课件)数字音频技术.ppt
- 清华大学出版社:《C语言程序设计》课程教学资源(PPT课件讲稿,共十二章,田丽华、岳俊华、孙颖馨).ppt
- 《算法设计与分析》课程教学资源(PPT讲稿)第十五讲 NP完全性理论与近似算法.pptx
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第八章 密钥分配与密钥管理.pptx
- 河南中医药大学(河南中医学院):《计算机网络》课程教学资源(PPT课件讲稿)第二章 物理层(阮晓龙).pptx
- 中国人民大学:A Survey on PIM(PPT讲稿).ppt
- 《电脑组装与维护实例教程》教学资源(PPT课件讲稿)第13章 计算机的保养.ppt
- 《轻松学习C语言》教学资源(PPT课件讲稿,繁体版,共十二章).pptx
- 《计算机组装维修及实训教程》课程教学资源(PPT课件)第2章 中央处理器.ppt
- 《操作系统》课程教学资源(PPT课件)第六章 设备管理 Devices Management.ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第三章 语法分析.ppt
- Object-Oriented Programming(Java).ppt
- Threads, SMP, and MicroKernels.ppt
- 对等网络 Peer-to-Peer Networks(P2P).ppt
- 香港浸会大学:《网络管理 Network Management》课程教学资源(PPT课件讲稿)Chapter 02 Network Management Model.ppt
- 中国科学技术大学:《高级操作系统 Advanced Operating System》课程教学资源(PPT课件讲稿)第四章 分布式进程和处理机管理(主讲:熊焰).ppt
- 兰州大学:《SOA & Web Service》教学资源(PPT课件讲稿)Lecture 5 Web Service Program(苏伟).ppt
- 哈尔滨工业大学:开放式中文实体关系抽取研究(导师:秦兵).pptx
- 《计算机控制技术》课程教学资源(PPT课件讲稿)第二章 模拟量输出通道.ppt
- 中国科学技术大学:《并行算法实践》课程教学资源(PPT课件讲稿)上篇 并行程序设计导论 单元I 并行程序设计基础 第三章 并行程序设计简介.ppt
- 《多媒体技术基础》课程教学资源(PPT课件讲稿)单元1 多媒体概述.ppt
- 广西医科大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Chapter 18 NETWORK DESIGN AND IMPLEMENTATION.pptx
- 《计算机网络》课程实验教学大纲.pdf
- 东南大学:《C++语言程序设计》课程教学资源(PPT课件讲稿)Chapter 11 Operator Overloading; String and Array Objects(主讲:东方).ppt
- 《大学计算机基础》课程教学资源(PPT课件讲稿)第四章 Excel 2007电子表格.ppt
- 进程(PPT课件讲稿)Processes.pptx
- 电子科技大学:《汇编语言程序设计》课程教学资源(PPT课件)第一章 基础知识(主讲:詹瑾瑜).ppt