Android learning notes-Intents and Intent Filters (2), androidintents

Source: Internet
Author: User

Android learning notes-Intents and Intent Filters (2), androidintents

My mailbox: JohnTsai.Work@gmail.com, welcome to discuss.

Welcome to reprint, reprint please note the web site: http://www.cnblogs.com/JohnTsai/p/3993488.html

  • Knowledge point:

Continue learning about Intents and Intent Filters yesterday

  • Build an Intent ):

The Intent object carries information (for example, a clear component name or an intent component type (intent category). Android uses these

Information determines which component to enable. It also carries the information used by the received components for full functionality execution.

Intent contains the following basic information:

1. component name

1 // 1. Component name (Component name) 2 // full restricted class name (fully qualified class name) of the target Component, including the package name of the app. 3 // For example, com. johntsai. SecondActivity4 // we can use the following method to set component name 5 setComponent () 6 setClass () 7 setClassName () 8 // or Intent Constructor

 

2. Functions

1 // 2. function, Activity 2 // specifies the function or activity string to be executed. 3 // you can specify the Intent Action by using the following method: 4 5 setAction () 6 // or Intent constructor 7 // if we construct our own action, we must include the package name of our app as the prefix 8 // For example: 9 static final String ACTION_TIMETRAVEL 10 = "com. johntsai. action. TIMETRAVEL ";

3. Data

1 // 3. data 2 // URI (Uri object) reference data or MIME-type data 3 // the data type provided is generally determined by the intent action 4 // For example, the action is ACTION_EDIT, data should contain the Editable document URI 5 // only set data URI 6 setData () 7 // only set the MIME type 8 setType () 9 // set both 10 setDataAndType () 11 // reminder: If you want to set both 2, you cannot call setData () or setType () because they will invalidate the values of the other party. SetDataAndType () is generally used ().

4. Category

1 // 4. category 2 // string containing the additional information about which component should process Intent 3 // A intent can have any number of category descriptions 4 // specify category5 addCategory ()

 

The component name, function, data, and type (component name, action, date, and category) represent the most typical features of an Intent,

By reading these attributes, the Android system can decide which component to enable.

5. Additional parts

1 // 5. extras 2 // Key-value paris) 3 // declare our own additional parts (Intent reception of our app) to ensure that our app package name is included as the prefix 4 // For example: 5 static final String EXTRA_GIGAWATTS = "com. johntsai. EXTRA_GIGAWATTS ";

6. Flag

1 // 6. flag (Flags) 2 // Flags guides Android system on how to enable Activity (for example, which task the Activity belongs to) and how to process it after it is enabled (for example, whether it belongs to the latest Activity list) 3 setFlags ();

If you like it, click here.

To be Continued .....


What is the role of intent in android ??

Intent and Intent Filters Intents and Intent Filters

The three core components of an application-activity. The service and broadcast receiver are activated through messages, that is, Intents. Intent message transmission is a mechanism that binds components in the same or different applications at a later time. Intent itself, an intent object, is a passive data structure that contains abstract descriptions of executed operations-or, for broadcast, a description of something that has occurred and been declared. There are different mechanisms for transmitting intent to each component:
• An intent object is passed to Context. startActivity () or Activity. startActivityForResult () to start an Activity or let an existing Activity do something new.
• An intent object is passed to Context. startService () to initiate a service or submit a new instruction to the running service. Similarly, an intent can be passed to Context. bindService () to establish a connection between a calling component and a target service. As an option, it can initiate this service if it is not yet running.
• Intent objects passed to any broadcast method (such as Context. sendBroadcast (), Context. sendOrderedBroadcast (), or Context. sendStickyBroadcast () are passed to all interested broadcast recipients. Many types of broadcast come from system code.
In each example, the Android system finds appropriate activities, services, or a group of broadcast receivers to respond to this intent and instantiate them if necessary. These message transmission systems do not overlap: the broadcast intent is transmitted only to the broadcast receiver and never to the activity or service. The intention of a message sent to startActivity () is to be transmitted only to an activity, never to a service or broadcast recipient, and so on.
This document starts with the description of the intent object, and then describes how Android maps the intent to the rule of the component-how to solve which component should receive an intent message. For intentions that do not explicitly name a target component, this process includes testing this intent object against the intent filter associated with the potential target.

Intent object Intent Objects
An Intent object is a pile of information. It contains information of interest to components that receive this intent (for example, the action and operation data to be taken) in addition, information of interest to the Android system (for example, the component category for which this intent should be processed and instructions for starting a target activity ):
Component name
The component name that should process this intent. this field is a ComponentName object-a composite: The fully qualified class name of the target component (such as "com. example. project. app. freneticActivity ") and the name of the package where the component is set in the application description file (for example," com. example. project "). the package name and the package name set in the description file do not have to match.
The component name is optional. If it is set, the intent object will be passed to the specified class. If not, Android uses the information in another Intent object to locate an appropriate target-see the Intent parsing Intent Resolution described later in this article.
The component name can be set by setComponent (), setClass (), or setClassName () and read through getComponent.

Several minor issues in Android Development

1. The first time you start the simulator, the speed is relatively slow, similar to starting the computer operating system, normal;
2. You do not need to restart the simulator to modify the code. You can re-run it in eclipse. The adt will automatically package your program into the simulator;

3. intent-filter:

Intent can be divided into Explicit intents and Implicit intents:

Explicit intents is an Intent that contains the Component Name (for example, a class containing the target Activity, such as new Intent (Activity1.class). For such Intent, only the specified class, that is, Activity1 can receive this Intent. That is to say, if you have an Intent to be switched to Activity1, and the Activity does not plan to receive other Intent, it makes no sense to add the intent filter to the Activity.

For Implicit intents, intent-filter is useful. Implicit intents is not called by a specific target, but can be called by all activities and Broadcast referers in the system. Therefore, the receiver must tell the receiver which Intent to receive, and the Intent to be received is filtered Based on the Intent Action, Category, and Data attributes, therefore, it is called Intent Filter.

For example, if you have a Broadcast Receiver, you want to listen to the Intent called by phone, that is, to receive the Intent whose Action is Phone_State_Change, the action of your intent-filter is Phone_State_Change (here is just an example. If you are too lazy to check the api, you may have written mistakes ).

I don't know if you understand this. We recommend you write a BroadcastReceiver yourself.

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.