Five basic components of Android applications

Source: Internet
Author: User
Tags change settings

1. Activity-application presentation layer (basic activity)

An activity represents a visual user interface and focuses on a user's events. For example, an activity may represent a list of menu items that a user can select, or a picture along with its title may be displayed. One text message applicationProgramThere may be an activity that shows the list of contacts to send information; The second activity, write information to the selected contacts; other activities, re-view the old information or change settings. Although they work together to form an overall user interface, each activity is independent of other activities. Every one isActivityThe implementation of a subclass of the base class.

Each screen in an application is implemented by inheriting and extending the base-class activity.

Each activity in the same application is independent of each other. The first screen displayed after the application starts is the first activity of the application (default window). Then, you can start another activity from the activity as needed.

Activity uses view to implement the GUI in the application (users directly interact with the application through GUI ). The visible content in the activity window is provided through the base class view. Use activity. setcontentview () to set the view object in the current activity.

L each view object controls a rectangle space in the window;

L view is a hierarchical structure. layout attributes in the parent view are inherited by the view;

L The rectangular space represented by the Child view object at the bottom of the view hierarchy is the place for interaction with users.

Activity Status callback:

L oncreate

L onstart

L onrestart

L onresume

L onpause

L onstop

L ondestroy

2. Service-no visible user interface, but can run on the background for a long time (Basic Service)

A Service does not have a visual user interface, but runs without deadline in the background. For example, a service may play background music while a user does other things, or it may expand data from the network, or compute something and provide the result to the desired activity (activities ). Each service is inherited fromServiceBase class.

The service does not block other components and user interfaces because it runs in the main thread of the application process.

The service cannot be started by itself. It must be started by calling the startservice or bindservice method through the context object (for example, an activity) (the service life cycle of the two methods is different ).

1. Call the startservice Method

A) if the service is not started, the oncreate method of the service is called first, and then the onstart method is called.

B) if the service has been started, the onstart method will be called directly.

C) The Service started by this method can be closed by calling stopservice through the context object, or by calling stopself () or stopselfresult () by the service itself. The ondestory method previously called can be closed.

2. Call the bindservice method to bind the current context object to the specified service through a serviceconnection object

A) if the service is not started, it will first call the oncreate method of the Service to initialize the start, and then call the onbind method of the Service to initialize the binding.

B) if the context object bound to the service is destroyed, the bound service will also call the onunbind and ondestroy methods to stop running.

C) Note: broadcastreceiver cannot be bound to a service.

D) The context object bound to a service can also be unbound from the service through unbindservice.

E) when the service is canceled, the service will call the unbind method. if the service is started through bindservice, The ondestroy method will be called to stop the service.

Service Status callback:

L oncreate

L onstart

L onbind

L onrebind

L onunbind

L ondestroy

3. Broadcast receiver-the component used by the user to receive broadcast notifications (base class broadcastreceiver)

A broadcast receiver is a component that does nothing but accepts broadcast announcements and responds accordingly. Many broadcasts originated from the systemCodeFor example, the announcement of time zone changes, low battery power, pictures have been taken, the user changed the language preferences. An application can also initiate a broadcast, for example, for other programs to know that some data has been downloaded to the device and they can use the data. An application can have any number of broadcast recipients to reflect any announcements that it deems important. All recipients inherit fromBroadcastreceiverBase class.

Broadcast in Android comes from either the system or common applications.

Many events may cause system broadcast, such as changes in the time zone of the mobile phone, low battery usage, and user changes in System Language settings.

A common application, such as an application, notifies other applications that some data has been downloaded.

To respond to different Event Notifications, applications can register different broadcast referers. All broadcast receivers inherit from the base class broadcastreceiver.

Broadcastreceiver itself does not implement a graphical user interface, but when it receives a notification, broadcastreceiver can start the activity as a response, or notify the user through icationicationmananger.

Broadcastreceiver is a component used to filter and receive and respond to broadcast.

Send broadcast information

1. Load the information to be sent and the information used for filtering (such as action and category) into an intent object.

2. Call the context. sendbroadcast (), sendorderbroadcast (), and sendstickybroadcast () Methods to broadcast the intent object.

3. Use the sendbroadcast () or sendstickybroadcast () method to send an intent. All broadcastreceiver that meets the conditions will randomly execute its onreceive () method;

4. The intent sent by sendorderbroadcast () will be executed according to the order of priority set by intentfilter during broadcastreceiver registration. The broadcastreceiver with the same priority will be executed randomly.

5. The main difference between sendstickybroadcast () and sendstickybroadcast () methods is that intent always exists after being sent, and this intent will be directly returned when registerreceiver () is called to register a matched intent later.

6. if the received permission is specified when the sendbroadcast () method is used, this is only available in androidmanifest. in XML, the <uses-Permission> label declares the broadcastreceiver with this permission to receive and send broadcast messages.

7. if you have specified the broadcast permission when registering broadcastreciever, only androidmanifest in the package is available. the <uses-Permission> label is declared in XML. Only the broadcast sent by the context object with this permission can be received by this broadcastreceiver.

Receive broadcast messages

1. inherit the broadcastreceiver class and implement the onreceive Method

2. register broadcast Uploader (two methods are available: one is to statically register broadcast Uploader in androidmanifest. in XML, the <author ER> label declaration is used, and the <intent-filter> label is used in the tag to set the filter. Another method is to dynamically define and set an intentfilter object in the code, then, you need to call context in the registration area. registerreceiver () method) (call context when canceling registration. unregisterreceiver () method)

4. content provider-to solve the problem of data communication and sharing between applications (base class contentprovider)

The content provider provides the specified dataset of an application to other applications. The data can be stored in a file system, in an SQLite database, or in any other reasonable way. The content provider inherits fromContentproviderThe base class implements a standard set of methods so that other applications can retrieve and store data. However, applications do not directly call these methods. Instead, they useContentresolverObject and call its method.ContentresolverCan communicate with any content provider. It cooperates with the provider to manage the communication between participating processes.

In Android, each application uses its own user ID and runs in its own process. The advantage is that it can effectively protect systems and applications and avoid being affected by other unusual applications. Each process has an independent process address space and virtual space.

Content provider can provide application-specific data to another application. The data storage method can be Android file system, SQLite database, or other reasonable methods.

When data needs to be shared between applications, we can use contentprovider to define a URI for data. When other applications query or modify data, they only need to obtain a contentresolver from the current context object, and then pass in the response Uri.

Content Provider inherits from the base class contentprovider and implements a set of standard interfaces. Through this interface, other applications can read, write, and store data. However, applications that need to use data do not call this set of methods directly, but call the contentresolver object method. The contentresolver object can communicate with any contentprovider.

To define a URI for the private data of the current application, you need to define a class that inherits the contentprovider, and then implement the functions of these methods according to the methods called by different operations.

The contentresolver class provides a method for applications to access the content mechanism. To construct a contentresolver object, you can pass in a context object for the constructor contentresolver (context), or call the getcontentresolver () method to obtain some contentresolver objects through the context object, you can operate data by calling its query (), insert (), update () and other methods.

Once the basic components of the above four Android applications are required to complete the request, Android will first confirm whether the process of the component is running. If not, Android will start the process first, check whether the requested component instance exists. Otherwise, a new component instance is created.

5. Intent-link connecting components

Among the above four basic components, except that the content provider is activated through content resolver, the activity, service, and broadcast referers of the other three components are activated by intent asynchronous messages.

Intent transmits messages between different components and transmits the request intent of one component to another. Therefore, intent is an object that contains specific request information.

For different components, intent contains different message content, and different components have different activation methods, and different types of components have different ways to transmit intent.

Intent is a runtime binding mechanism, which can be used to connect two different components while the program is running. With intent, your program can send a request or willingness to the android table. Android selects an appropriate component to process the request based on the desired content.

L activate a new activity or let an existing activity execute a new operation, you can call the following two methods (the intent parameter required for these two methods is called activity action intent ):

1. Context. startactivity ()

2. activity. startactivityforresult ()

L to start a new service or send new commands to an existing service, you can call the following two methods:

1. Context. startservice ()

2. Context. bindservice ()

L send broadcast intent (all registered broadcastreceiv with intenfilter matching will be activated). You can call the following three methods:

1. Context. sendbroadcast ()

2. Context. sendorderbroadcast ()

3. Context. sendstickbroadcast ()

Once the intent is sent, Android will accurately find one or more matching activities, services, or broadcastreceiver in response. Therefore, different types of intent messages do not overlap. broadcastintent messages are only sent to broadcastreceiver, but cannot be sent to activity or server. A message transmitted by startactivity () can only be sent to activity. Intent transmitted by startservice () can only be sent to service.

Related Article

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.