Summary In this chapter we covered a broad swath of Android territory. We first focused on the

4.5 Summary In this chapter we covered a broad swath of Android territory. We first focused on the

Intent abstraction, defining what intents are, how they are resolved using Intent- Filter objects, and what some built-in platform-provided Intent handlers are. We also addressed explicit Intent invocation versus implicit Intent invocation and the reasons you might choose one type over another. In that discussion we completed the RestaurantFinder sample application.

After we covered the basics of Intent classes, we moved on to a new sample appli- cation, WeatherReporter. Within the scope of this application, we explored the con- cept of a BroadcastReceiver and an Android Service. We used the receiver to start the Service, and we designed the Service to send notification alerts for severe weather events. Along with Service implementation details we covered the difference between starting and binding services and the moving parts behind the Android IPC system, which uses the Android IDL process.

Through looking at all these components in several complete examples, you should now have a good idea of the basic foundation of these concepts. In the next chapter we will build on this foundation a bit further by looking at the various means Android provides to retrieve and store data, including using preferences, the file sys- tem, databases, and creating a ContentProvider.

Storing and retrieving data

This chapter covers:

Storing and retrieving data with SharedPreferences

Using the filesystem

Working with a SQLite database

Accessing and building a ContentProvider

Anytime you are developing software, one of the most common and basic con- structs you have to deal with is the means to store and retrieve data. It’s all about the data after all. Though there are many ways to pipe data into and out of various languages and technologies, there are typically only a few ways to persist it: in mem- ory structures, the filesystem, databases, and network services.

Like other technologies, Android has its own concepts for getting and sharing data in applications, yet these concepts are ultimately implemented using famil- iar approaches (for the most part). Android provides access to the filesystem, has support for a local relational database through SQLite, and includes a Shared- Preferences object and preferences system that allows you to store simple key- value pairs within applications.

Using preferences

In this chapter we are going to take a tour of each of the local data-related mecha- nisms (we will examine the network possibilities in chapter 6). We will start with pref- erences and create a small sample application to exercise those concepts. From there we will create another sample application to examine using the filesystem to store data, both internal to our application and external using the platform’s SD card sup- port. Then we will look at creating and accessing a database. To do this we will take a closer look at some of the code and concepts from the WeatherReporter application we created in chapter 4, which uses SQLite.

Beyond the basics, Android also includes its own construct that allows applications to share data through a clever URI-based approach called a ContentProvider. This technique combines several other Android concepts, such as the URI-based style of intents and the Cursor result set seen in SQLite, to make data accessible across differ- ent applications. To demonstrate how this works we will create another small sample application that uses built-in providers, then we will walk through the steps required to create a ContentProvider on our own.

We begin with the easiest form of data storage and retrieval Android provides, preferences.