The five basic components of Android

Source: Internet
Author: User

Basic components of Android
1.ActivityActivity is the most basic module, commonly referred to as "activity", in the application generally an activity is a separate screen. Each activity is implemented as a separate class and inherits from the active base class, which displays the user interface consisting of the view control and responds to the event. Activity uses view to implement the GUI in the application (the user interacts directly with the GUI and the application). The visible content within the Activity window is provided through the base Class View. Use the Activity.setcontentview () method to set the View object in the current activity.Each view object controls a rectangular space within the window;View is a hierarchical structure in which the layout properties in the Parent view are inherited by the quilt view;The rectangular space represented by the child view object at the bottom of the view hierarchy is the place to interact with the user
activity status callback:
onstart Onresume onpause OnDestroy
2.intent

of the other 4 basic components, except that content provider is activated through content resolver, the other 3 components of activity, service, and broadcast receiver are activated by intent asynchronous messages.

The intent passes messages between different components, passing the request intent of one component to another component. Therefore, intent is the object that contains the specific request information.

For different components, the intent contains different message content, and different components are activated differently, and different types of components have different ways of passing intent.

Intent is a runtime binding mechanism that connects two different components during a program's run. With intent, your program can send Android to a request or wish, and Android will choose the right component to handle the request, depending on the intended content.

Activating a new activity, or having an existing activity perform a new operation, can be called by calling the following two methods (these two rollup methods require an incoming Intent parameter known as activity Action Intent):

1.context.startactivity ()

2.activity.startactivityforresult ()

To start a new service, or to pass a new instruction to an existing service, you can call the following two methods:

1.context.startservice ()

2.context.bindservice ()

Send broadcast Intent (all registered BROADCASTRECEIV with matching intenfilter will be activated), you can call the following three ways:

1.context.sendbroadcast ()

2.context.sendorderbroadcast ()

3.context.sendstickbroadcast ()

Intent once issued, Android will find exactly one or more of the matching activity, service, or broadcastreceiver response. Therefore, different types of intent messages do not overlap, and broadcastintent messages are sent only to broadcastreceiver and never to the activity or server. Messages delivered with startactivity () can only be sent to activity, and intent passed by StartService () can only be sent to the service.

The intent object abstractly describes the main components of the execution operation, intent;

1. Target component name. [Available options]

A) The component name is a ComponentName object that is a combination of the Target component class name and the application package where the target component resides

b) The package name in the component does not have to exactly match the package name in the Manifes file

c) If the name of the target component is indicated in the intent message, this is a display message and intent is passed to the specified component.

D) If the target component name is not specified, Android selects the appropriate target component through the comparison of other information within the intent and the registered Intentfilter

2. Action [Implicit comparison]

A) A string describing the name of the action triggered by intent.

b) In theory the action can be any string, whereas the action string associated with the Android system application is defined in the intent class as a static string constant.

3.Data [Implicit comparison]

A) describes the URI and data type of the data to be manipulated by the intent.

b) Setting intent data correctly is important for Android to look for components that match intent requests in the system.

4.Category [Implicit comparison]

A) is the additional descriptive information for the requested component.

b) Android also defines a set of static string constants in the intent class that represent different categories of intent.

5.Extra

A) When we use intent to connect different components, we sometimes need to attach additional information to the intent in order to pass the data to the target activity.

b) Extra is stored in the intent object with a key-value pair, and the intent object stores and obtains Getextras by invoking the method Putextras () and extra ()

c) Extra is saved as a bundle object, and the bundle object provides a series of put and get methods to set and extract the corresponding key value information.

d) static string constants are defined for some of the extra's key values in the intent class that also apply to Android systems.

6.Flag

Determine the factors that intent the target component:

In an explicit intent message, the only element that determines the target component is the component name (no further intent content is defined)

In an implicit intent message, because there is no target component name, the Android system must help the application find the component that best matches the intent of the intent request.

The target component in an implicit intent message is specifically selected by Android to compare the intent request content with a filter called Intentfilter, which contains all the possible components to be selected in the system. If a component in intentfilter matches the implicit intent request content, then Android chooses the component as the target component of the implicit intent.

Intenfilter

Components of an application to tell Android that it can respond and handle implicit intent requests, you can declare one or more intentfilter.

Each intentfilter describes the ability of the component to respond to intent requests-what type of request behavior the component wants to receive, and what kind of request data.

Implicit intent and Intentfilter compare three elements: Action, Data, Category.

An implicit intent request to be able to pass to a target component must pass the above three checks. If any aspect does not match, Android will not pass the implicit intent to the target component.

<intent-filter>

<action android:name= ""/>

<category android:name= ""/>

<data android:type= "" Android:scheme= "" android:authority= "" android:path= ""/>

</intent-filter>

1. Action Test

A) A <intent-filter> should contain at least one <action>, otherwise no intent request can match the <intent-filter>.

b) If no Actino type is included in the Intentfilter, then no intent request can match this intentfilter.

c) If the action type is not set in the intent request, the intent request will pass Intentfilter test as long as the action type is included in the Intentfilter.

2. Category Testing

A) The intent request passes the test only if all of the category in the intent request exactly matches the category of one of the intentfilter in the component The redundant category declaration in Intentfilter does not cause the match to fail.

b) A intentfilter that does not specify any category will only match intent requests that do not have a category set.

3. Data testing

A) The <data> element specifies the data URI and data type of the intent request that you want to accept: The URI is divided into three categories for matching, scheme, authority, and path.

b) The URI data type and scheme for the intent request set with SetData must be consistent with the specified in the Intentfilter

3.Service runs in the main thread of the application process, so the Service does not block other components and the user interface.

The service cannot be started on its own, and must be started by invoking the StartService or Bindservice method through a context object (such as an activity) (the service's life cycle is different from the two methods).

1. Call the StartService method

A) If the service does not start, the service's OnCreate method is called first, and then the OnStart method is called.

b) If the service is already started, the OnStart method is called directly

c) The service initiated by the method can be closed by invoking StopService with the context object, or it can be closed by invoking stopself () or Stopselfresult () on the service itself, and the Ondestory method is called before closing.

2. Call the Bindservice method so that the current context object is bound to the service specified by a Serviceconnection object

A) If the service does not start, the service's OnCreate method is called first to initialize the boot, and then the service's Onbind method is invoked to initialize the binding.

b) If the context object that binds the service is destroyed, the bound service also calls the Onunbind and OnDestroy methods to stop running

c) Note: Broadcastreceiver is not bound to a service.

d) A context object that is bound to a service can also use Unbindservice () to unbind the services.

e) When canceled, the service calls the Unbind method, and the OnDestroy method is called to stop the services, if they are started by Bindservice.

Service Status callback:

L OnCreate

L OnStart

L Onbind

L Onrebind

L Onunbind

L OnDestroy
4.Broadcast ReceiverThe broadcasts in Android are either from the system or from a common application.
Many events can result in system broadcasts, such as changes in the time zone of the phone, low battery level, and user changes to system language settings. Some data from a common application, such as an application that notifies other applications, has already been downloaded. To respond to different event notifications, the application can register a different broadcast Receiver. All broadcast receiver inherit from the base class Broadcastreceiver.The broadcastreceiver itself does not implement a graphical user interface, but when it receives a notification, Broadcastreceiver can initiate activity as a response or alert the user through Notificationmananger. Broadcastreceiver is a class of components that are filtered to receive and respond to a sent broadcast.Send broadcast information1. Load the information to be sent and the information used for filtering (such as action, Category) into a intent object2. Call the Context.sendbroadcast (), Sendorderbroadcast (), Sendstickybroadcast () method to broadcast the intent object3. Using the Sendbroadcast () or Sendstickybroadcast () method to emit the intent, all broadcastreceiver that satisfy the condition will execute its onreceive () method randomly;4. The Sendorderbroadcast () intent will be executed according to the order of precedence Intentfilter set at the time of Broadcastreceiver registration, Broadcastreceiver with the same priority is randomly executedThe main difference of the 5.sendStickyBroadcast () method is that the intent is present after it is sent, and this intent is returned directly when Registerreceiver () is registered for the matching intent.6. If you specify a permission to receive when using the Sendbroadcast () method, this is only used in Androidmanifest.xml <uses-permission> The label declares that a broadcastreceiver with this permission is likely to receive a send to broadcast.7. If you specify the permission to receive broadcast when registering Broadcastreciever, only the <uses-permission> tag is declared in the Androidmanifest.xml within the package, The broadcast sent by the context object with this permission is only possible to be received by this broadcastreceiver.Receive broadcast messages1. Inherit the Broadcastreceiver class and implement the OnReceive method2. Register broadcast Receiver (there are 2 ways: One method is to statically use the <receiver> tag declaration in the Androidmanifest.xml and use it in the tag <intent-filter> Tag settings filter, another way to dynamically define and set up a Intentfilter object in code, and then call the Context.registerreceiver () method where you want to register) (when you unregister, Call the Context.unregisterreceiver () method)
5.Content Provider
To solve the problem of data communication and sharing between applications (base class ContentProvider)In Android, each application is run with its own user ID and in its own process. The benefit is that the system and applications can be effectively protected from other unhealthy applications, each with a separate process address space and virtual space.Content Provider can provide application-specific data for use by another application. The data can be stored in an Android file system, SQLite database, or any other logical way. When data needs to be shared between applications, we can use ContentProvider to define a URI for the data. Then, when the other application queries or modifies the data, it only needs to get a contentresolver from the current context object and then pass in the URI of the response. The Content Provider inherits from the base class ContentProvider and implements a standard set of interfaces. Through this set of interfaces, other applications can read and write data and store it. However, applications that need to use data do not call this set of methods directly, but rather by invoking the method of the Contentresolver object. The Contentresolver object can communicate with any contentprovider. to define URIs for the private data of the current application, you need to specifically define a class that inherits from ContentProvider, and then implement the functions of these methods according to the methods called by the different operations. The Contentresolver class provides a way for applications to access the content mechanism. To construct a Contentresolver object you can pass in a context object for the constructor method Contentresolver (context context), or you can call directly through the context object Getcontentresolver () method to obtain-some Contentresolver objects, you can call their query (), insert (), update () and other methods to manipulate the data.

Reprinted from Http://blog.csdn.net/CrazyJeff_Liu/archive/2010/04/30/5545806.aspx

The five basic components of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.