Android Development Tour 5: Application Basics and Components

Source: Internet
Author: User

Introduction

Previous Android Development tour: Application Basics and Components describes the basics of the application and the four components of Android, this article describes how to activate a group shutdown component. The topics in this article are as follows:

    • 1. Activating the component: Intention (Intents)
      • 1.1. Activation of the active (activity) component
      • 1.2. Activation of service Components
      • 1.3. Activation of the broadcast receiver (broadcast receiver) component
    • 2. Turn off the components
    • 3. manifest file
    • 4. Intent Filter
1. Activating the component: Intention (Intents)

When a request is received from the contentresolver , the content provider is activated. The other three components-activity, service, and broadcast receivers-are activated by an asynchronous message called intent (intent) . Intent is a Intent object that holds the content of the message. for activities and services , the intent object indicates the requested action name and the URI and other information that is the data for the action object. For example, it can pass a request to an activity, let it display a picture for the user, or let the user edit some text. for broadcast receivers , the intent object indicates the behavior of the broadcast. For example, when the camera button is pressed, it can broadcast to all objects of interest.

The method of activation is different for each component. The activation methods for the activity, service, and broadcast recipient components are described below.

1.1. Activation of the active (activity) component

An activity is loaded (or assigned a new job) by passing a intent object to context.startactivity () or activity.startactivityforresult () . The corresponding activity can see the initial intent, and this intent is to view the activation activity through the getintent () method. Android calls the active onnewintent () method to pass any subsequent intent.

An activity often starts the next one. If it expects the activity it initiates to return a result, it calls Startactivityforresult () instead of startactivity (). For example, if it launches an activity that lets the user pick a photo, it may return the selected photo. The result is a intent object that passes the Onactivityresult () method that invokes the activity.

1.2. Activation of service Components

Start a service (or give a new instruction to a running service) by passing a intent object to Context.startservice () . Android invokes the OnStart () method of the service and passes the intent object to it.

Similarly, a intent can be passed to Context.bindservice () to establish a continuous connection between the called component and the target service. This service will accept the intent object in the call to the Onbind () method (if the service has not started,Bindservice () will start it first). For example, an activity can connect to the music playback service described earlier and provide the user with an actionable (user interface) to control playback. This activity can call Bindservice () to establish a connection, and then invoke the object defined in the service to control playback.

1.3. Activation of the broadcast receiver (broadcast receiver) component

An application can pass a intent object to the

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

and other similar methods to generate a broadcast. Android passes intent through the onreceive () method to all broadcast recipients who are interested in this broadcast.

2. Turn off the components

The content provider is only activated when responding to Contentresolver requests. A broadcast receiver is activated only when it responds to broadcast messages. Therefore, it is not necessary to explicitly close these components.

The activity is different, it provides the user interface. Session with the user, so as long as the session continues, even if the conversation process is idle, it will remain active. Similarly, the service will remain operational for a long period of time. So Android provides an orderly way to shut down activities and services.

    • You can close an activity by calling its finish () method. An activity can also close another activity (it starts with Startactivityforresult ()) by calling the finishactivity () method.
    • The service can stop by calling its stopself () method, or call Context.stopservice ().

The component may also be shut down by the system when the component is no longer in use or when Android has to reclaim memory for more active components.

3. List (manifest) file

Before Android launches an application component, it must know that the component is present. So, the application declares its component in a manifest (manifest) file, which is packaged in an Android package. this. apk file will also include the application's code, files, and other resources.

This manifest file is an XML-structured file, and all Android applications call it Androidmanifest.xml. To declare an application component, it also does a lot of extra work, such as specifying the name of the library that the application needs to link to (in addition to the default Android library) and declaring the various permissions that the application expects to get.

But the main feature of the manifest file is still the component that declares the application to Android. For example, an activity can be declared as follows:

<?xml 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 <activity> element specifies the subclass of the activity class that implements the activities,icon and label property points to the resource file that contains the icon and label for this activity that is presented to the user.

Other components are also declared in a similar way-the<service> element is used to declare the service, and the<receiver> element is used to declare the broadcast receiver, and <provider> element is used to declare a content provider. Activities, services, and content providers that are not declared in the manifest file will not be visible to the system and thus will not be run. However, broadcast receivers can be declared either in the manifest file or dynamically in code (as BroadcastReceiver objects) and called Context.registerreceiver () to register to the system.

4. Intent Filter

The intent object can explicitly specify the target component. If this designation is made, Android will find the component (according to the declaration in the manifest file) and activate it. But if intent is not explicitly specified, Android must find the component that is most appropriate for intent. This process is done by comparing the intent object with the intent filter for all possible objects . The intent filter for the component tells Android what type of intent it can handle. They are declared in the manifest file, just like other necessary information about the component. Here is an extension of the above example, which includes two intent filter declarations for the activity:

 <?xml version= "1.0" encoding= "Utf-8"? ><manifest ... > <appli cation > <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> & Lt;intent-filter > <action android:name= "Com.example.project.BOUNCE"/> & Lt;data android:mimetype= "Image/jpeg"/> <category android:name= "Android.intent.category.DEFAULT"/ > </intent-filter> </activity> </APPLICATION></MANIFEST&G t;  

The first filter in the example--action: The combination of "Android.intent.action.MAIN" and Category: "Android.intent.category.LAUNCHER" is common. It marks the list of bootable applications that the activity displays in the Application Launcher, which the user sees on the device. In other words, this activity is the portal to the application and is the first activity that the user sees when they choose to run the application. The second filter declares that the activity is for a specific type of data.

A component can have any number of intent filters, each declaring a range of different capabilities. If it does not contain any filters, it will only be explicitly declared with intent activation of the target component name.

For broadcast receivers, it creates and registers a intent filter in code that is instantiated directly as an object of Intentfilter. Other filters are set in the manifest file.

If you're not fully aware of these concepts right now, I'm just going to give you the impression that you know the existence of these concepts or terminology and what they might be doing. I will go back to these things in more detail and combine some examples, and you will know these things clearly by then.

Android Development Tour 5: Application Basics and Components

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.