1 / 35

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Dis

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Discover! Persistent Data. Objectives. In this chapter, you learn to: Create an Android project using persistent data U nderstand different types of persistent data

adolph
Download Presentation

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Dis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Discover! Persistent Data Android Boot Camp for Developers Using Java, 2nd Ed.

  2. 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  3. Objectives (continued) • Write data using SharedPreferences • Instantiate a SharePreferences object • Write data using getString( ) method • Retrieve data from SharedPreferences • Read data using a putString( ) method • Display an ImageView control using cod Android Boot Camp for Developers Using Java, 2nd Ed.

  4. Using Persistent Data (continued) Steps to complete the App: • Add strings to the String table. • Add images to the drawable folder. • Design two XML layouts for the first and second Activity. • Instantiate the XML controls in the first Activity. • Establish a SharedPreferences object to store the data entered. • Write data to the SharedPreferences object. • Launch a second Activity. • Initialize the XML controls on the second Activity. • Retrieve the data from the SharedPreferences object. • Determine the status of the frequent flier using an If structure. • Display the results on the second Activity. Android Boot Camp for Developers Using Java, 2nd Ed.

  5. Using 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  6. Using 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  7. Using Persistent Data (continued) • 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  8. Using Persistent Data (continued) • 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. Android Boot Camp for Developers Using Java, 2nd Ed.

  9. Using Persistent Data (continued) • 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: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> Android Boot Camp for Developers Using Java, 2nd Ed.

  10. Using Persistent Data (continued) • 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  11. Using Persistent Data (continued) • 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. Android Boot Camp for Developers Using Java, 2nd Ed.

  12. Creating XML Layout Files • Adding images, a String table and two XML layout files Android Boot Camp for Developers Using Java, 2nd Ed.

  13. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  14. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  15. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  16. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  17. Creating XML Layout Files (continued) • Instantiating the XML Controls Android Boot Camp for Developers Using Java, 2nd Ed.

  18. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  19. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  20. Creating XML Layout Files (continued) • Writing Persistent Data with SharedPreferences • 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  21. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  22. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  23. Creating XML Layout Files (continued) • Launching the Second Activity Android Boot Camp for Developers Using Java, 2nd Ed.

  24. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  25. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  26. Creating XML Layout Files (continued) • Instantiating the Second Activity Controls Android Boot Camp for Developers Using Java, 2nd Ed.

  27. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  28. Creating XML Layout Files (continued) • Retrieving Preferences • Retrieving Android data is just as easy as saving it when you are working with SharedPreferences • 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  29. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  30. Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  31. Coding an ImageView Control • 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  32. Coding an ImageView Control (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  33. Coding an ImageView Control (continued) Android Boot Camp for Developers Using Java, 2nd Ed.

  34. 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 Android Boot Camp for Developers Using Java, 2nd Ed.

  35. 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. Android Boot Camp for Developers Using Java, 2nd Ed.

More Related