Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android A...
Download
1 / 35

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Di - PowerPoint PPT Presentation


  • 349 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 11: Di' - adolph


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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.


Objectives
Objectives A Guide to Creating Your First Android Apps

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.


Objectives continued
Objectives A Guide to Creating Your First Android Apps(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.


Using persistent data continued
Using Persistent A Guide to Creating Your First Android AppsData (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.


Using persistent data continued1
Using A Guide to Creating Your First Android AppsPersistent 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.


Using persistent data continued2
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Using persistent data continued3
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Using persistent data continued4
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Using persistent data continued5
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Using persistent data continued6
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Using persistent data continued7
Using Persistent Data A Guide to Creating Your First Android Apps(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.


Creating xml layout files
Creating XML Layout Files A Guide to Creating Your First Android Apps

  • Adding images, a String table and two XML layout files

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued1
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued2
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued3
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued4
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

  • Instantiating the XML Controls

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued5
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued6
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued7
Creating XML Layout Files A Guide to Creating Your First Android Apps(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.


Creating xml layout files continued8
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued9
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued10
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

  • Launching the Second Activity

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued11
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued12
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued13
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

  • Instantiating the Second Activity Controls

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued14
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued15
Creating XML Layout Files A Guide to Creating Your First Android Apps(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.


Creating xml layout files continued16
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Creating xml layout files continued17
Creating XML Layout Files A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Coding an imageview control
Coding an ImageView Control A Guide to Creating Your First Android Apps

  • 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.


Coding an imageview control continued
Coding an ImageView A Guide to Creating Your First Android AppsControl (continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Coding an imageview control continued1
Coding an ImageView Control A Guide to Creating Your First Android Apps(continued)

Android Boot Camp for Developers Using Java, 2nd Ed.


Summary
Summary A Guide to Creating Your First Android Apps

  • 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.


Summary continued
Summary A Guide to Creating Your First Android Apps(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.


ad