Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Exploring local data storage options
- Creating an Android virtual device
- Starting a new project
- Defining preferences with Java and activities
- Creating and reading JSON and XML data files
- Creating a new SQLite database
- Inserting and retrieving data in the database
Skill Level Intermediate
The Android SDK includes many options for storing data locally with an Android app. All of these options give you the ability to persist information between application launches or between device sessions--that is after a device is shutdown and then restarted--because in all of the options that I'm going to describe, you'll be saving files to local flash memory or other persistent media. Here are the three major options for local data storage: shared preferences, arbitrary files that you simply create and save to disk, or SQLite relational databases.
Here is a little bit more detail about each of the options. A shared preference is simply a key-value pair, a key, which is a string, and then a value, which can be one of many data types. It's up to you as the Android application developer or designer to decide what preferences are needed for your application. Typically, a preference is used either to present specific information to the user or to determine the behavior or appearance of an application.
You can manage your preferences in one of two ways, either programmatically with Java using a special API that's a part of the Android SDK, or through a preference activity, a specific kind of user interface that's defined by the SDK and will manage your preferences for you. Regardless of whether you do it programmatically or through an activity, preferences are stored unencrypted. They're stored in a simple XML file on your persistent data storage area, and the creation and then reading of the XML content is handled completely by the underlying API.
As the developer, you'll create your preferences either programmatically or through an activity, and then you'll be able to read them programmatically with Java. But the mechanics of the XML file are handled for you. Shared preferences are great for very simple values, but when you need to store more complex information, you might want to go to the next option: creating your own files. The Android SDK has a complete system for creating, storing, and reading files, and you can create and read these files using the persistent media that's a part of the Android device.
There aren't any special file types. You can create pretty much anything, including binary files like images, XML, JSON, or delimited data files, or anything else for which you can program. You'll be able to choose from a variety of approaches to creating and reading these files, including APIs that are unique to Android or other APIs that are available in all implementations of Java. You can control access to these files by where you place them in your storage.
You can designate a file to be internal, which means it can only be accessed by the application that created it, or external, which means that it's available to other applications as well. Finally, if you need the power of a complete relational database, you can create an SQLite file. An SQLite database is a complete relational database that can store multiple database tables and has statically typed columns, so you can designate a data type as being a string, an integer, a double, and so on.
SQLite is a very powerful little database, and it's used on a lot of different platforms, not just Android. You can find complete information about SQLite from the homepage: www.sqlite.org. But the most important information you'll need will be in the documentation for this package: android.database.sqlite. Android's implementation of SQLite is pretty plain vanilla, but what's important is the programming interface that's provided.
It lets you standardize how SQLite files are created and where they're placed on disk. When you create an SQLite database file programmatically, the database will be stored locally and it will be associated with the application, so later on, when the application is uninstalled, the associated database file will be deleted with it. If you want to share this sort of complex data with other applications on a device, consider creating a custom content provider. And I'll show you how to do that toward the end of this course.
All Android devices, regardless of whether they are cell phones or tablets, are networked, and so the final way that you can deal with data is through the network. You can store application data on a networked server and then manage your network communications using a variety of APIs. You can use java.net, which is a part of the core Java language, or android.net, a set of classes that are unique to Android. And if you're programming for Honeycomb, Android 3 or later, you can use a class called Loader, that greatly simplifies the loading of data from a networked server.
Data exchange between an Android device and a networked data source is typically handled using what are called restful web services: simple XML files that are requested and retrieved from a networked server and then pushed back up to the server to make requests. If you're familiar with SOAP-based web services, you won't be able to use those so easily in Android because there's no built-in support for them. This course, however, focuses on local data storage, and so the use of networked data is outside the scope of the course.
But let's get started with local data storage with Android.