Getting started with Android development: how to activate and disable components

Source: Internet
Author: User

This article describes how to activate a group to close components. The topic of this article is as follows:

  • 1. Activate the component: Intents)
    • 1.1. Activity) component Activation
    • 1.2 Service) component Activation
    • 1.3 Broadcast receiver) component Activation
  • 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 activated by an asynchronous message called intent. The Intent is an Intent object that stores the message content. For activities and services, the 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 the broadcast receiver, the Intent object indicates 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. Activity) component Activation

By passing 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 Service) component Activation

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 the onBind () method call. 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 an operable user interface to control playback. This activity can call bindService () to establish a connection, and then call the objects defined in the service to control playback.

1.3 Broadcast receiver) component Activation

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 files

Before Android starts an application component, it must know that the component exists. Therefore, the application declares its components in a list 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, in addition to 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. xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest . . . >    
  3.     <application . . . > 
  4.          <activity android:name="com.example.project.FreneticActivity" 
  5.                    android:icon="@drawable/small_pic.png" 
  6.                    android:label="@string/freneticLabel" 
  7.                     . . .  > 
  8.          </activity> 
  9.           . . . 
  10.      </application> 
  11. </manifest> 

<Activity> 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 declare the service in a similar way. The <service> element declares the broadcast receiver, and the <provider> element declares 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 either declare it in the configuration file or dynamically create it as a BroadcastReceiver object in the Code) and register it to the system by calling Context. registerReceiver.

4. Intent Filter

The Intent object can explicitly specify the target component. If this is done, Android will find this component based on the declaration in the configuration file) and activate it.

However, if the Intent is not explicitly specified, Android must find the most appropriate component for the intent. This process is done 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. xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest . . . >    
  3.     <application . . . > 
  4.          <activity android:name="com.example.project.FreneticActivity" 
  5.                    android:icon="@drawable/small_pic.png" 
  6.                    android:label="@string/freneticLabel" 
  7.                     . . .  > 
  8.               <intent-filter . . . > 
  9.                   <action android:name="android.intent.action.MAIN" /> 
  10.                   <category android:name="android.intent.category.LAUNCHER" /> 
  11.               </intent-filter> 
  12.               <intent-filter . . . > 
  13.                   <action android:name="com.example.project.BOUNCE" /> 
  14.                   <data android:mimeType="image/jpeg" /> 
  15.                   <category android:name="android.intent.category.DEFAULT" /> 
  16.               </intent-filter> 
  17.          </activity> 
  18.           . . . 
  19.      </application> 
  20. </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.

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.