420 likes | 488 Views
Android Boot Camp for Developers Using Java, 3E 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 U nderstand SharedPreferences persistent data
E N D
Android Boot Camp for Developers Using Java, 3E Chapter 11: Discover! Persistent Data Android Boot Camp for Developers Using Java, 3rd Ed.
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, 3rd Ed.
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 Android Boot Camp for Developers Using Java, 3rd Ed.
Understanding Persistent Data 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. • Calculate the monthly payment and display the appropriate image for the number of loan years using an If structure. • Display the monthly payment on the second Activity. Android Boot Camp for Developers Using Java, 3rd Ed.
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 Android Boot Camp for Developers Using Java, 3rd Ed.
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 Android Boot Camp for Developers Using Java, 3rd Ed.
Understanding 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, 3rd Ed.
Understanding 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, 3rd Ed.
Understanding 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, 3rd Ed.
Understanding 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, 3rd Ed.
Understanding 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, 3rd Ed.
Creating XML Layout Files Android Boot Camp for Developers Using Java, 3rd Ed.
Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Creating XML Layout Files (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Creating a Second Activity and XML Layout Android Boot Camp for Developers Using Java, 3rd Ed.
Creating a Second Activity and XML Layout (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Instantiating the XML Controls Android Boot Camp for Developers Using Java, 3rd Ed.
Instantiating the XML Controls (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Instantiating the XML Controls (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
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, 3rd Ed.
Writing Persistent Data with SharedPreferences(continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Writing Persistent Data with SharedPreferences(continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Launching the Second Activity Android Boot Camp for Developers Using Java, 3rd Ed.
Instantiating the Second Activity Controls Android Boot Camp for Developers Using Java, 3rd Ed.
Retrieving Preferences • 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 Android Boot Camp for Developers Using Java, 3rd Ed.
Retrieving Preferences (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Retrieving Preferences (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
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, 3rd Ed.
Coding an ImageView Control (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Coding an ImageView Control (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
Coding an ImageView Control (continued) Android Boot Camp for Developers Using Java, 3rd Ed.
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, 3rd Ed.
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, 3rd Ed.