Friday, 16 July 2010 16:50

Learning the Fundamental Components

Rate this item
(0 votes)
Every application framework has some key components that developers need to understand before they can begin to write applications based on the framework. For example, you would need to understand JavaServer Pages (JSP) and servlets in order to write Java 2 Platform, Enterprise Edition (J2EE) applications. Similarly, you need to understand activities, views, intents, content providers, services, and the AndroidManifest.xml file when you build applications for Android. We will briefly discuss these fundamental concepts here so that you can follow the rest of this chapter, and we’ll discuss them in more detail throughout the book.


View


The concept of a view in J2EE and Swing carries over to Android. Views are UI elements that form the basic building blocks of a user interface. Views are hierarchical and they know how to draw themselves.

Activity

An activity is a user interface concept. An activity usually represents a single screen in your application. It generally contains one or more views, but it doesn’t have to. Moreover, other concepts in Android could better represent a viewless activity (as you’ll see in the “Service” section shortly).

Intent

An intent generically defines an “intention” to do some work. Intents encapsulate several concepts, so the best approach to understanding them is to see examples of their use. You can use intents to perform the following tasks, for instance:

• Broadcast a message
• Start a service
• Launch an activity
• Display a web page or a list of contacts
• Dial a phone number or answer a phone call

Intents are not always initiated by your application—they’re also used by the system to notify your application of specific events (such as the arrival of a text message).

Intents can be explicit or implicit. If you simply say that you want to display a URL, the system will decide what component will fulfill the intention. You can also provide specific information about what should handle the intention. Intents loosely couple the action and action handler.


Content Provider


Data sharing among mobile applications on a device is common. Therefore, Android defines a
standard mechanism for applications to share data (such as a list of contacts) without exposing the underlying storage, structure, and implementation. Through content providers, you can expose your data and have your applications use data from other applications.

Service

Services in Android resemble services you see in Windows or other platforms—they’re background processes that can potentially run for a long time. Android defines two types of services: local services and remote services. Local services are components that are only accessible by the application that is hosting the service. Conversely, remote services are services that are meant to be accessed remotely by other applications running on the device.

An example of a service is a component that is used by an e-mail application to poll for new messages. This kind of service might be a local service if the service is not used by other applications running on the device. If several applications use the service, then the service would be implemented as a remote service. The difference, as you’ll see in Chapter 8, is in startService() vs. bindService().

You can use existing services and also write your own services by extending the Service class.


AndroidManifest.xml

AndroidManifest.xml, which is similar to the web.xml file in the J2EE world, defines the contents and behavior of your application. For example, it lists your app’s activities and services,along with the permissions the application needs to run.


Hello World!

Now you’re ready to build your first Android application. You’ll start by building a simple “Hello World!” program. Create the skeleton of the application by following these steps:

1. Launch Eclipse and select File ? New ? Project. In the “New Project” dialog box, select “Android” and then click “Next.” You will then see the “New Android Project” dialog box, as shown in Figure 2-4.


2. As shown in Figure 2-4, enter HelloAndroid as the project name, pro.android as the package name, HelloActivity as the activity name, and HelloAndroidApp as the application name. Note that for a real application, you’ll want to use a meaningful application name because it will appear in the application’s title bar. Also note that the default location for the project will be derived from the Eclipse workspace location. In this case, your Eclipse workspace is c:\Android, and the New Project Wizard appends the name of the new application to the workspace location to come up with
c:\Android\HelloAndroid\.

3. Click the “Finish” button, which tells ADT to generate the project skeleton for you. For now, open the HelloActivity.java file under the src folder and modify the onCreate() method as follows:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** create a TextView and write Hello World! */
TextView tv = new TextView(this);
tv.setText("Hello World!");
/** set the content view to the TextView */
setContentView(tv);
}
Add an import statement for android.widget.TextView. To run the application, you’ll need to create an Eclipse launch configuration (see Figure 2-5).


Create the Eclipse launch configuration by following these steps:

1. Select Run ? Run Configurations.
2. In the “Run Configurations” dialog box, double-click “Android Application” in the left
pane. The wizard will insert a new configuration named “New Configuration.”
3. Rename the configuration RunHelloWorld.
4. Click the “Browse…” button and select the HelloAndroid project.
5. Under “Launch Action,” select “Launch” and select “pro.android.HelloActivity” from the drop-down list.
6. Click “Apply” and then “Run.” You should see the emulator launched with the HelloAndroid project (see Figure 2-6).   


Now you know how to create a new Android application and run it in the emulator. Next, we’ll discuss the pieces that make the simple program display in the emulator. We’ll begin by talking about an Android application’s artifacts and structure.




 
Last modified on Tuesday, 30 November 1999 10:00
John

John


E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it