[Translation] Android Application basics> application components)

Source: Internet
Author: User

Android applicationsProgramIt is mainly written in Java. Android provides a tool named aapt to package compiled Java binary files (. Class) and required data and resources into an APK package. You can install this APK package on Android devices. An APK package is considered an application.
 

From many perspectives, every android application is alive in an independent environment:

1) Each application runs in its own Linux Process by default.

2) each process has an independent Virtual Machine (VM). Therefore, each application runs in an isolated island environment compared to other applications.

3) Each application is assigned a unique Linux User ID by default. Because of the role of permissions, application resources are only visible to end users and themselves. Of course, there are also ways to expose application resources to other applications for use.
 

It is also possible that two applications share the same user ID. In this case, they can access each other's resources. In addition, for the purpose of saving system resources, multiple applications with the same user ID can run in the same Linux Process and share the same VM.
 

1. Application Components)
 

One of the features of Android is that your application can use the elements of other applications (the application that needs to provide the elements is allowed ). Your application does not need to contain or link to an application that provides elements. You only need to start the corresponding elements of other applications as needed.
To support this feature, when an application element is used by another application, the system needs to start the process of the application and initialize the Java object of the corresponding element. Therefore, the Android app portal does not have only one main portal for some applications on other systems. Android apps have four core components that can be initialized and run by the system as appropriate:

 

1) Activity
 

An activity represents a visual user interface. The base classes of all activities in the program areAndroid. App. Activity.Each activity is assigned a default window for painting. In general, the window will fill the whole screen, but it can also be smaller than the screen size, floating on other windows. You can also use other windows for an activity. For example, a dialog box is displayed in the center of an activity, or an important message is displayed after you perform an operation.

The visible content is represented by the view Hierarchy Tree.Android. View. ViewIs their base class. Each view object controls a rectangular area within the window range. The parent view object contains and organizes its child view object layout. The view object located on the leaf node is drawn in the rectangle area it controls and responds to user behavior in this area. Therefore, view is the place where the activity interacts with the user.
Input the root node of the view Hierarchy TreeActivity. setcontentview ()You can draw the content of the view Hierarchy Tree in the corresponding activity window.

 

2) Service

 

The service component has no direct relationship with the user interface. It runs in the background. The base classes of all services areAndroid. App. Service.

We can connect to (BIND) A Running Service (if this service is not running, start this service ). After the connection, we can use the interface exposed by the Service to interact with the service.

Same as other components. The service runs in the main thread of the application process. To avoid blocking the running of other components, the service often starts another thread to complete some time-consuming work.

 

3) broadcast Receiver

 

The broadcast Receiver component is used to receive and respond to broadcast messages. Many broadcast messages are sent by the system, but applications can also send broadcast messages.

All broadcast referers are fromAndroid. content. broadcastreceiverInherited. After receiving a broadcast message, you can start an activity or display a notification to attract your attention.

 

4) content provider

 

Content provider can expose your application resources to other applications. All content providers are inherited fromAndroid. content. contentprovider. Other applications that want to use your application resources will not directly use the content provider in your application, but willAndroid. content. contentresolverObject To interact with the content provider in your application. It can be seen that the combination of contentresolver and content provider can solve the problem of inter-process communication.

When a request needs to be processed by a specific component, the android system will ensure that the application process with this component is running, it also makes sure that the corresponding object of the component exists.

 

2. Activate the component: Intent

 

When contentresolver sends a request, the corresponding target content provider component is activated. The other three components are activated by an asynchronous message called "intent. An intent message carries information.Android. content. IntentObject. For activity and service components, the intent is to name the requested activity, specify the uri of the operated data, and some other information. For the broadcast Receiver component, the intent is to name the broadcast activity.

 

To activate different components, you need to call different methods:

 

2.1 to activate the activity, you need to pass in an intent objectContext. startactivity ()Method orActivity. startactivityforresult ()Method. The activated activity can be called.Activity. getintent ()Method to obtain the intent object used to activate this activity. If an intent object is passed to an activated activity, the android system callsOnnewintent ()Method, and the intent object is passed in.

To return results from an activated activity, you must callActivity. startactivityforresult ()Method to activate the activity and override the previous activity'sActivity. onactivityresult ()Method.

 

2.2 import an intent objectContext. startservice ()You can activate a service component (or pass new commands to a running service component ). Android system will call this serviceService. onstart ()Method, and the intent object is passed in.

Similarly, you can pass in an intent objectContext. bindservice ()Method to establish a connection with a running service component. The Android system callsService. onbind ()(If the target service component is not running, the bindservice () method can selectively start the target service component ).

 

2.3 import an intent objectContext. sendbroadcast (),Context. sendorderedbroadcast (), OrContext. sendstickybroadcast ()In the method and its variant methods, the application can initiate a notification message. The Android system delivers the intent object to all interested broadcast referers and callsBroadcastreceiver. onreceive ()Method.

 

3. disable components

 

As mentioned above, we do not need to explicitly close the content provider and broadcast receiver components.

We can callActivity. Finish ()Method to close the activity itself. Of course, we can also callActivity. finishactivity (INT)To close yourself (this activity is called by other activitiesActivity. startactivityforresult ()).

For service components, we can callService. stopself ()Method To close itself, we can also callContext. stopservice (intent)To close the service component.

In addition, when the component is no longer used or the Android system has a low available memory value, the android system may also disable the component.

 

4. manifest File

 

The application needs to declare its own components in the manifest file so that the Android system can know the existence of the components (activity, service, and content provider must be declared in this file; in addition to declaring in this file, the broadcast receiver can also call context. the registerreceiver () method is dynamically registered in the program ). In applications, the manifest file name is always androidmanifest. XML, in addition to declaring components, this file also has some other functions. For example, it describes the additional libraries of the application link (in addition to the libraries of the android default link) and describes the permissions of the program.

 

Activity:<Activity...> </activity>

Service:<Service...> </service>

Content Provider:<Provider...> </provider>

Broadcast receiver er:<Cycler...> </Cycler>

 

5. Intent plug-in

 

The intent object can explicitly describe the name of the target component. Android can find the corresponding component and activate it by checking the component name declared in the manifest file. However, the intent object can also not specify the name of the target component. In this way, the android system needs to set the intent filters of the intent object and the component declared in the manifest file (intent plug-in) and find the most appropriate component, and then activate the component. Therefore, intent filter is used to inform Android system of the type of intent objects that can be processed by this component. Intent filter is also declared in the manifest file (one component can be absent or multiple intent-filters can be included ):

    <  Intent-Filter  ...  >  
< Action Android: Name = "Android. Intent. Action. Main" />
< Category Android: Name = "Android. Intent. Category. launcher" />
</ Intent-Filter >

 

The followingIntent-FilterIndicates that a component can process a specific data type.

  <  Intent-Filter  ...  >  
< Action Android: Name = "Com. example. Project. Bounce" />
< Data Android: mimetype = "Image/JPEG" />
< Category Android: Name = "Android. Intent. Category. Default" />
</ Intent-Filter >

for the broadcast Receiver component dynamically registered in Code , you can declare android. content. intentfilter object to represent an intent-filter.

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.