Application Components

来源:互联网 发布:如何消灭软件红蜘蛛 编辑:程序博客网 时间:2024/06/06 08:34

Application components are the essential buildingblocks of an Android application. Each component is a different point throughwhich the system can enter your application. Not all components are actualentry points for the user and some depend on each other, but each one exists asits own entity and plays a specific role—each one is a unique building blockthat helps define your application's overall behavior.

There arefour different types of application components. Each type serves a distinctpurpose and has a distinct lifecycle that defines how the component is createdand destroyed.

Here arethe four types of application components:

Activities

An activity represents a single screen with a userinterface. For example, an email application might have one activity that showsa list of new emails, another activity to compose an email, and anotheractivity for reading emails. Although the activities work together to form acohesive user experience in the email application, each one is independent ofthe others. As such, a different application can start any one of theseactivities (if the email application allows it). For example, a camera applicationcan start the activity in the email application that composes new mail, inorder for the user to share a picture.

Anactivity is implemented as a subclass of Activity andyou can learn more about it in the Activities developer guide.

Services

service is a component that runs in thebackground to perform long-running operations or to perform work for remoteprocesses. A service does not provide a user interface. For example, a servicemight play music in the background while the user is in a differentapplication, or it might fetch data over the network without blocking userinteraction with an activity. Another component, such as an activity, can startthe service and let it run or bind to it in order to interact with it.

A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.

Content providers

contentprovider manages a shared setof application data. You can store the data in the file system, an SQLitedatabase, on the web, or any other persistent storage location your applicationcan access. Through the content provider, other applications can query or evenmodify the data (if the content provider allows it). For example, the Androidsystem provides a content provider that manages the user's contact information.As such, any application with the proper permissions can query part of thecontent provider (such as ContactsContract.Data)to read and write information about a particularperson.

Content providers are also useful for reading and writingdata that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.

A content provider is implemented as a subclass of ContentProvider andmust implement a standard set of APIs that enable other applications to performtransactions. For more information, see the ContentProviders developer guide.

Broadcast receivers

A broadcast receiver is a component that responds tosystem-wide broadcast announcements. Many broadcasts originate from thesystem—for example, a broadcast announcing that the screen has turned off, thebattery is low, or a picture was captured. Applications can also initiatebroadcasts—for example, to let other applications know that some data has beendownloaded to the device and is available for them to use. Although broadcastreceivers don't display a user interface, they may create astatus bar notification toalert the user when a broadcast event occurs. More commonly, though, abroadcast receiver is just a "gateway" to other components and isintended to do a very minimal amount of work. For instance, it might initiate aservice to perform some work based on the event.

A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.For more information, see the BroadcastReceiver class.

       A unique aspect of theAndroid system design is that any application can start another application’scomponent. For example, if you want the user to capture a photo with the devicecamera, there's probably another application that does that and yourapplication can use it, instead of developing an activity to capture a photoyourself. You don't need to incorporate or even link to the code from thecamera application. Instead, you can simply start the activity in the cameraapplication that captures a photo. When complete, the photo is even returned toyour application so you can use it. To the user, it seems as if the camera isactually a part of your application.

       When thesystem starts a component, it starts the process for that application (if it'snot already running) and instantiates the classes needed for the component. Forexample, if your application starts the activity in the camera application thatcaptures a photo, that activity runs in the process that belongs to the cameraapplication, not in your application's process. Therefore, unlike applicationson most other systems, Android applications don't have a single entry point (there'sno main() function,for example).

       Because the systemruns each application in a separate process with file permissions that restrictaccess to other applications, your application cannot directly activate acomponent from another application. The Android system, however, can. So, toactivate a component in another application, you must deliver a message to thesystem that specifies your intent to start a particular component. Thesystem then activates the component for you.

Activating Components

       Three ofthe four component types—activities, services, and broadcast receivers—areactivated by an asynchronous message called an intent. Intents bindindividual components to each other at runtime (you can think of them as themessengers that request an action from other components), whether the componentbelongs to your application or another.

       Anintent is created with an Intent object, which defines a message to activate either a specificcomponent or a specific type of component—an intent can be eitherexplicit or implicit, respectively.

       Foractivities and services, an intent defines the action to perform (for example,to "view" or "send" something) and may specify the URI ofthe data to act on (among other things that the component being started mightneed to know). For example, an intent might convey a request for an activity toshow an image or to open a web page. In some cases, you can start an activityto receive a result, in which case, the activity also returns the result in an Intent (for example, you can issue an intentto let the user pick a personal contact and have it returned to you—the returnintent includes a URI pointing to the chosen contact).

       Forbroadcast receivers, the intent simply defines the announcement being broadcast(for example, a broadcast to indicate the device battery is low includes only aknown action string that indicates "battery is low").

       Theother component type, content provider, is not activated by intents. Rather, itis activated when targeted by a request fromContentResolver. The content resolver handles all directtransactions with the content provider so that the component that's performingtransactions with the provider doesn't need to and instead calls methods onthe ContentResolver object.This leaves a layer of abstraction between the content provider and thecomponent requesting information (for security).

There areseparate methods for activating each type of component:

Youcan start an activity (or give it something new to do) by passingan Intent to startActivity() or startActivityForResult() (when you want theactivity to return a result).

Youcan start a service (or give new instructions to an ongoing service) by passingan Intent to startService(). Or you can bind to theservice by passing an Intent tobindService().

You can initiate a broadcast by passing an Intent to methods like sendBroadcast()sendOrderedBroadcast(), or sendStickyBroadcast().

Youcan perform a query to a content provider by calling query() on a ContentResolver.

        For more information about using intents, seethe Intents andIntent Filters document.More information about activating specific components is also provided in thefollowingdocuments: ActivitiesServicesBroadcastReceiver and ContentProviders.

 

0 0