Android development journey: Application basics and components (continued)

Source: Internet
Author: User

-- Success is a person who can withstand loneliness. You are one step closer to success.

Introduction

This article introduces the basic knowledge of the application and the four components of Android. This article describes how to activate and disable components in groups. The topic of this article is as follows:

  • 1. Activate the component: intents)
    • 1.1 activate the activity component
    • 1.2 activation of Service Components
    • 1.3. Activation of the broadcast Receiver component
  • 2. Close Components
  • 3. inventory file
  • 4. Intent Filter
1. Activate the component: intents)

After receiving a request from contentresolver, the content provider is activated. The other three components, activity, service, and broadcast receiver, are calledIntention (Intent).Activate. The intent is an intent object that stores the message content.For activities and servicesThe intent object specifies the request operation name and the URI and other information of the data used as the operation object. For example, it can send a request to the activity so that it displays an image for the user or allows the user to edit some text.For broadcast recipientsThe intent object specifies the broadcast behavior. For example, when a photo button is pressed, it can broadcast all objects of interest.

For each component, the activation method is different. The following describes how to activate the activity, service, and broadcast receiver components.

1.1 activate the activity component

You can pass an intent object to context. startactivity () or activity. startactivityforresult () to load (or specify a new job to) an activity. The initial intent is displayed for the corresponding activity. The intent is viewed through the getintent () method. Android calls the onnewintent () method of the activity to pass any subsequent intent.

An activity often starts the next one. If it expects that the activity it starts to return a result, it will call startactivityforresult () instead of startactivity (). For example, if it starts an activity for users to select a photo, it may return the selected photo. The result is transmitted as an intent object to call the onactivityresult () method of the activity.

1.2 activation of Service Components

By passing an intent object to context. startservice () to start a service (or giving a new command to a running service ). Android calls the onstart () method of the service and passes the intent object to it.

Similarly, an intent can be passed to context. bindservice () to establish a continuous connection between the called component and the target service. This service will accept this intent object in calling the onbind () method (if the service has not been started, bindservice () will start it first ). For example, an activity can be connected to the music playing Service mentioned above and provide users with an operable (User Interface) to control playing. This activity can call bindservice () to establish a connection, and then call the objects defined in the service to control playback.

1.3. Activation of the broadcast Receiver component

Applications can pass intent objects

  • Context. sendbroadcast ()
  • Context. sendorderedbroadcast ()
  • Context. sendstickybroadcast ()

And other similar methods to generate a broadcast. Android will pass the intent to all broadcast recipients interested in this broadcast through the onreceive () method.

2. Close Components

The content provider is activated only when it responds to the contentresolver request. A broadcast receiver is activated only when it responds to broadcast information. Therefore, there is no need to explicitly close these components.

Different activities provide user interfaces. Sessions with the user, so as long as the session continues, even if the conversation process is idle, it will remain active. Similarly, the service is also running for a long period of time. Therefore, Android provides methods to close activities and services in an orderly manner.

  • You can call its finish () method to close an activity. An activity can also be closed by calling the finishactivity () method (it is started with startactivityforresult ).
  • The service can be stopped by calling its stopself () method, or calling context. stopservice ().

When a component is no longer used or android must reclaim memory for more active components, the component may also be disabled by the system.

3. List (manifest) File

Before Android starts an application component, it must know that the component exists. Therefore, the application declares its components in a manifest file, which is packaged into the android package. This .apk file also includes application code, files, and other resources.

This configuration file is an XML file, and all android applications call it androidmanifest. xml. To declare an application component, it also performs a lot of extra work, such as specifying the name of the library to which the application is linked (except for the default Android library) and declare the various permissions the application expects.

However, the main function of the configuration file is to declare the application components to Android. For example, an activity can be declared as follows:

AndroidManifest.xmlxml version="1.0" encoding="utf-8"?>manifest . . . >       application . . . >         activity android:name="com.example.project.FreneticActivity"                   android:icon="@drawable/small_pic.png"                   android:label="@string/freneticLabel"                    . . .  >          activity>          . . .      application>manifest>

The name attribute of the element specifies the subclass of the activity class that implements the activity. The icon and label attributes point to the resource file that contains the icons and labels displayed to the user.

Other components are declared in a similar way -- An element is used to declare a service, Element is used to declare the broadcast receiver, while Element is used to declare the content provider. Activities, services, and content providers that are not declared in the list file will not be seen by the system and thus will not be run. However, the broadcast receiver can be declared in the inventory file or dynamically created in the Code (BroadcastReceiverObject) and the system is registered by calling context. registerreceiver.

4. Intent Filter

The intent object can explicitly specify the target component. If this type is specified, Android finds this component (based on the declaration in the configuration file) and activates it. However, if the intent is not explicitly specified, android must find the most appropriate component for the intent.This process is completed by comparing intent objects and intent filters of all possible objects.. The intent filter of the component informs android of the intent type it can process. Like other necessary information about components, they are declared in the inventory file. Here is an extension of the above example, which includes two intent filter declarations for the activity:

AndroidManifest.xmlxml version="1.0" encoding="utf-8"?>manifest . . . >       application . . . >         activity android:name="com.example.project.FreneticActivity"                   android:icon="@drawable/small_pic.png"                   android:label="@string/freneticLabel"                    . . .  >               intent-filter . . . >                  action android:name="android.intent.action.MAIN" />                  category android:name="android.intent.category.LAUNCHER" />              intent-filter>              intent-filter . . . >                  action android:name="com.example.project.BOUNCE" />                  data android:mimeType="image/jpeg" />                  category android:name="android.intent.category.DEFAULT" />              intent-filter>         activity>          . . .      application>manifest>

The first filter in the example -- Action: "Android. Intent. Action. Main" and category: "Android. Intent. Category. launcher" are common. It indicates that this activity is displayed in the application initiator, and the list of started applications that the user sees on the device. In other words, this activity is the entry to the application and the first activity you see after you select to run the application. The second filter declares that this activity targets specific types of data.

A component can have any number of intent filters, each of which declares a series of different capabilities. If it does not contain any filter, it can only be activated with an intent that explicitly declares the name of the target component.

For the broadcast receiver, it creates and registers the intent filter in the Code and directly instantiates the intentfilter object. Other filters are set in the configuration file.

 

If you do not fully understand these concepts, I will only give you an impression on the existence of these concepts or terms and what they do. Later, I will introduce these things in more detail and combine them with some instances, so that you will know these things clearly.

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.