当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

西安电子科技大学:《Mobile Programming》课程PPT教学课件(Android Programming)Lecture 7 数据持久化 Data Persistence

资源类别:文库,文档格式:PPTX,文档页数:51,文件大小:274.56KB,团购合买
点击下载完整版文档(PPTX)

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 non￾volatile 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

点击下载完整版文档(PPTX)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共51页,可试读17页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有