Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

Similar presentations


Presentation on theme: "© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp."— Presentation transcript:

1 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp for Developers Using Java, 3E Chapter 11: Discover! Persistent Data 1 Android Boot Camp for Developers Using Java, 3rd Ed.

2 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives In this chapter, you learn to: Create an Android project using persistent data Understand different types of persistent data Understand SharedPreferences persistent data Understand internal storage Understand external storage Understand saving data using a network connection Understand saving to a database connection 2 Android Boot Camp for Developers Using Java, 3rd Ed.

3 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives (continued) Write data using a SharedPreferences object Instantiate a SharePreferences object Write data using getString( ) method Retrieve data from a SharedPreferences object Read data using a putString( ) method Display an ImageView control using code 3 Android Boot Camp for Developers Using Java, 3rd Ed.

4 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data Steps to complete the App: 1.Add strings to the String table. 2.Add images to the drawable folder. 3.Design two XML layouts for the first and second Activity. 4.Instantiate the XML controls in the first Activity. 5.Establish a SharedPreferences object to store the data entered. 6.Write data to the SharedPreferences object. 7.Launch a second Activity. 8.Initialize the XML controls on the second Activity. 9.Retrieve the data from the SharedPreferences object. 10.Calculate the monthly payment and display the appropriate image for the number of loan years using an If structure. 11.Display the monthly payment on the second Activity. 4 Android Boot Camp for Developers Using Java, 3rd Ed.

5 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) Persistent data stores values permanently by placing the information in a file Can be stored in five different ways in Android applications 5 Android Boot Camp for Developers Using Java, 3rd Ed.

6 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) Shared preferences –Stores private data in key–value pairs Internal storage –Stores private data in the memory of the device External storage –Stores data, which can be available to other apps on shared external storage SQLite database –Stores structured data in a private database Network connection –Stores data on a Web server 6 Android Boot Camp for Developers Using Java, 3rd Ed.

7 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 7 Android Boot Camp for Developers Using Java, 3rd Ed. Using Shared Preferences –Can save any data including user preferences, such as what wallpaper a user has chosen or individual values entered by the user in an EditText control –Can be used to save any primitive data: Booleans, floats, ints, longs, and strings

8 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 8 Android Boot Camp for Developers Using Java, 3rd Ed. Using Internal Storage –Store the information directly on the device’s internal drive –Saved files on the device are available only to the app that created the files –Be careful - low internal storage space can drastically affect the speed of an Android device and battery life

9 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 9 Android Boot Camp for Developers Using Java, 3rd Ed. Using External Storage –Store the information on the device’s SD (Secure Digital) card –All applications can read and write files placed on the external storage and the Android smartphone or tablet owner can remove them –To use external storage, the following permissions are necessary in the Android Manifest file:

10 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 10 Android Boot Camp for Developers Using Java, 3rd Ed. Using SQLite Databases –Perfect for a large amount of data –Available since the Cupcake 1.5 version of Android and occupies a small amount of disk memory –Android apps model data items in tables and columns, with optional relationships between the entities within the database –The tables can be queried using SQL statements

11 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Persistent Data (continued) 11 Android Boot Camp for Developers Using Java, 3rd Ed. Using a Network Connection –If connected to the Internet (a 3G/4G or Wi-Fi connection), data can be stored and retrieved on a Web service –If a connection is not available, the user cannot save or retrieve the persistent data

12 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files 12 Android Boot Camp for Developers Using Java, 3rd Ed.

13 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files (continued) 13 Android Boot Camp for Developers Using Java, 3rd Ed.

14 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating XML Layout Files (continued) 14 Android Boot Camp for Developers Using Java, 3rd Ed.

15 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating a Second Activity and XML Layout 15 Android Boot Camp for Developers Using Java, 3rd Ed.

16 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Creating a Second Activity and XML Layout (continued) 16 Android Boot Camp for Developers Using Java, 3rd Ed.

17 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls 17 Android Boot Camp for Developers Using Java, 3rd Ed.

18 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls (continued) 18 Android Boot Camp for Developers Using Java, 3rd Ed.

19 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the XML Controls (continued) 19 Android Boot Camp for Developers Using Java, 3rd Ed.

20 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Writing Persistent Data with SharedPreferences 20 Android Boot Camp for Developers Using Java, 3rd Ed. Data is saved to an XML file as a key–value pair –The key is a string such as “key1” that uniquely identifies the preference –The value is the data represented as a string, int, long, float, or Boolean Data types supported by the SharedPreferences class: –putString( )—Stores string values –putInt( )—Stores integer values –putLong( )—Stores long values –putFloat( )—Stores float values –putBoolean( )—Stores Boolean values

21 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Writing Persistent Data with SharedPreferences (continued) 21 Android Boot Camp for Developers Using Java, 3rd Ed.

22 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22 Android Boot Camp for Developers Using Java, 3rd Ed. Writing Persistent Data with SharedPreferences (continued)

23 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23 Android Boot Camp for Developers Using Java, 3rd Ed. Launching the Second Activity

24 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Instantiating the Second Activity Controls 24 Android Boot Camp for Developers Using Java, 3rd Ed.

25 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences 25 Android Boot Camp for Developers Using Java, 3rd Ed. Retrieve the SharedPreferences object and use the appropriate method to retrieve a key’s value by name –getString( )—Retrieves string values –getInt( )—Retrieves integer values –getLong( )—Retrieves long values –getFloat( )—Retrieves float values –getBoolean( )—Retrieves Boolean values

26 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences (continued) 26 Android Boot Camp for Developers Using Java, 3rd Ed.

27 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Retrieving Preferences (continued) 27 Android Boot Camp for Developers Using Java, 3rd Ed.

28 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control 28 Android Boot Camp for Developers Using Java, 3rd Ed. An ImageView control can display an image by assigning a source path (android:src="drawable/filename") in the XML layout file or by dynamically assigning the image within the Java code

29 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 29 Android Boot Camp for Developers Using Java, 3rd Ed.

30 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 30 Android Boot Camp for Developers Using Java, 3rd Ed.

31 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Coding an ImageView Control (continued) 31 Android Boot Camp for Developers Using Java, 3rd Ed.

32 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Summary When data users enter in an Android app is stored in RAM, it is lost when the app or the device stops running –Persistent data, on the other hand, is stored on the device’s drive or other storage medium such as a memory card or cloud service so that the data can be retrieved later within the app or after the termination of the program Persistent data can be stored using shared preferences, internal storage, external storage, a SQLite database, or a network connection –Use the SharedPreferences object to save any primitive data: Booleans, floats, ints, longs, and strings 32 Android Boot Camp for Developers Using Java, 3rd Ed.

33 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Summary (continued) When you save application data using the SharedPreferences object, the data is saved to an XML file as a key–value pair –The key is a string such as “key1” that uniquely identifies the preference, and the value is the data represented as a string, int, long, float, or Boolean. You can use the key–value pairs stored in SharedPreferences in different Activities of your application or in another application Use a putDataType( ) method to store the data in a SharedPreferences object, and use a getDataType( ) method to retrieve the data. 33 Android Boot Camp for Developers Using Java, 3rd Ed.


Download ppt "© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp."

Similar presentations


Ads by Google