Testing SMS

The emulator has a built-in set of tools for manipulating certain telephony behavior to simulate a variety of conditions, such as in and out of network coverage and placing phone calls. This section's example demonstrated another feature of the emulator, the receipt of an SMS message.

To send an SMS message to the emulator, telnet to port 5554 (the port # may vary on your system), which will connect to the emulator and issue the following command at the prompt:

sms send <sender's phone number> <body of text message>

To learn more about available commands, type help from the prompt. These tools are discussed in more detail in chapter 2.

ContentProvider is used. The ContentProvider implements a standard set of methods to permit an application to access a data store. The access may be for read and/or write operations. A ContentProvider may provide data to an Activity or Service in the same containing application as well as an Activity or Service contained in other applications.

A ContentProvider may use any form of data storage mechanism available on the Android platform, including files, SQLite databases, or even a memory-based hash map if data persistence is not required. In essence, the ContentProvider is a data layer providing data abstraction for its clients and centralizing storage and retrieval routines in a single place.

Directly sharing files or databases is discouraged on the Android platform and is further enforced by the Linux security system, which prevents ad hoc file access from one application space to another without explicitly granted permissions.

Data stored in a ContentProvider may be of traditional data types such as integers and strings. Content providers can also manage binary data such as image data. When binary data is retrieved, suggested practice is to return a string representing the filename containing the binary data. In the event a filename is returned as part of a ContentProvider query, the file should not be accessed directly, but rather you should use the helper class, ContentResolver's openInputStream method, to access the binary data. This approach negates Linux process/security hurdles as well as keeps all data access normalized through the ContentProvider. Figure 1.6 outlines the relationship among ContentProviders, data stores, and their clients.

A ContentProvider's data is accessed through the familiar Content URI. A ContentProvider defines this as a public static final String. For example, an application might have a data store managing material safety data sheets. The Content URI for this ContentProvider might look like this:

public static final Uri CONTENT_URI =

Uri.parse("content://com.msi.manning.provider.unlockingandroid/datasheets");

Android application #3

Activity 3.1

Activity 3.1

Data With Tiered Relationship
Figure 1.6 The content provider is the data tier for Android applications and is the prescribed manner in which data is accessed and shared on the device.

From this point, accessing a ContentProvider is similar to using Structured Query Language (SQL) in other platforms, though a complete SQL statement is not employed. A query is submitted to the ContentProvider, including the columns desired and optional Where and Order By clauses. For those familiar with parameterized queries in SQL, parameter substitution is even supported. Results are returned in the Cursor class, of course. A detailed ContentProvider example is provided in chapter 5.

NOTE In many ways, a ContentProvider acts like a database server. While an application could contain only a ContentProvider and in essence be a database server, a ContentProvider is typically a component of a larger Android application that hosts at least one Activity, Service, and/or BroadcastReceiver.

This concludes the brief introduction to the major Android application classes. Gaining an understanding of these classes and how they work together is an important aspect of Android development. Getting application components to work together can be a daunting task. For example, have you ever had a piece of software that just didn't work properly on your computer? Perhaps it was copied and not installed properly. Every software platform has environmental concerns, though they vary by platform. For example, when connecting to a remote resource such as a database server or FTP server, which username and password should you use? What about the necessary libraries to run your application? These are all topics related to software deployment. Each Android application requires a file named

AndroidManifest.xml, which ties together the necessary pieces to run an Android application on a device.

0 0

Post a comment