Use intent and intentfilter for communication

Source: Internet
Author: User

Intent Chinese translation is "intent ". Android uses intent to encapsulate the call intention of a program. Intent provides a unified programming model and can play the role of decoupling.

How to use an intent object to start system components:

Startactivity (intent)

Startactivityforresult (intent, int requestcode)

Startservice (intent)

Bindservice (intent, serviceconnection Conn, int flags)

Sendbroadcast (intent)

Sendstickybroadcast (intent) and so on

 

Common attributes of intent objects

An intent object consists of the following six parts:

Component

Action

Data

Category

Extras

Flags

Two Methods for intent to find the target component

Type 1: explicit call, which is directly specified through the Component Attribute;

The intent object that specifies the Component Attribute already specifies which component to start, and almost no longer needs to be used in <intent-filter…> So the intent is also an explicit intent.

ComponentName com = new ComponentName("com.antroid.Test", "com.android.Test.TestActivity"); Intent  intent = new Intent(); intent.setComponent(com); startActivity(intent);
Componentname comp = getintent. getcomponent (); // query the componentname information log. V ("MSG", comp. getpackagename + "" + Comp. getclassname) on the target page );

 

 

Type 2: implicit call. If the name of the target component is not explicitly specified, filter by certain conditions.

In the <intent-filter... /> Configure the following attributes:

0 ~ N <actions... />

0 ~ N <category... />

0 ~ 1 <data... />

 

1. Action

Action refers to the action to be completed by intent and is a String constant. Many action constants are defined in the intent class, including:

For example, intent. setaction (intent. acyion_call );

Of course, you can also define the action constant by yourself. You need to add the package name of your application as the prefix for the custom constant.

When multiple action elements are contained, the activity can respond to the intent whose action attribute value is any one of the strings.

 

2. Category

Category is also a string that provides additional information. Some category constants are also defined in the intent class:

 

Note that each <intent-filter... /> The component can contain multiple <action... /> And <category... /> Child element. Each intent object can only specify one action requirement, but multiple category requirements can be specified. As long as the requirement of the component is greater than or equal to the requirement specified by the intent, the intent can start the component.

If the category attribute is not specified for an intent object, the intent object starts the component whose category attribute value is intent. category_dafult by default.

 

3. Data

The data attribute is usually used to provide operation data to the action attribute. The data property accepts a URI object. For example, when action is action_edit, the data field will be the document URI; when action is action_call, the data field will be Tel: URI with the phone number to be called; if action is action_view, the data domain is http: URI.

Such as: http://www.baidu.com Tel: 13800138000

 

4. Extra attributes

It is used for data exchange between multiple actions and can carry bundle objects.

Use intent and intentfilter for communication

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.