Android API Guides Learn--intent and Intent Filters

Source: Internet
Author: User

Intent is a message-passing class that can start some components

Role:

1 Start an activity component.

The activity component is started by the startactivity()/ startactivity () method of the context class.

2 Starting a service component

Starting this component through the method of the context class startService() , you can only perform a one-time operation (such as downloading a file).

Through the bindservice () method of the context class

3 Send a broadcast

Through the context class sendBroadcast() , the sendOrderedBroadcast() orsendStickyBroadcast()给别的组件发送消息。

Intent Type:

1 Explicit Intent:

When you know the component you want to start, specify the class name that will launch the component (you need to create a specific component). For example, downloading files in the background, initiating an activity to respond to user behavior.

When you use a display intent to start a component, the detailed intent attribute is specified in the intent class (except for the component name, other properties are optional).

In general, an explicit intent is used to initiate special activity and service.

2 Implicit intent:

A component name is not explicitly specified. A component that is typically used to invoke another app in the app.

When you use implicit intent, the Android system goes to find the right component to boot. If intent matches intent filter, the component is launched and intent is passed to the intent class, and if more than one intent filter is met, the popup dialog box lets the user choose.

通过resolveActivity()方法可判断是否查找到合适的组件。

Intent filter is a statement in the manifest file that describes the various types of intent that a component wants to receive.

To ensure that your app is secure, do not declare intent filter to the service when you start a service using explicit intent. Using an implicit intent to start a service is a security risk. Because you are not sure which service will respond to intent, the user will not see which service is booting.

Create a Intent class

Main information to be carried by the intent class:

Component Name:

The name of the component that will be started.

This property is selectable.

But if you want to create an explicit component, then you need to determine the component name. (Explicit intent)

If you are unsure of the component name, the system decides which component to start with intent other properties. (Implicit intent)

When starting the service component, it is best to have a clear component name in order to ensure that your app is secure.

You can use the intent constructor method, or, to setComponent() setClass() setClassName() determine the name of the component that will be started.

Action:

Determines the action that will be performed by a string. For example, view or select.

You can define a string of action properties in your app for you to use, but you should take precedence over the defined Action property strings in the intent class or other framework classes.

ACTION_VIEW: When you have some information that needs to be displayed to the user, such as displaying an address in the map app

ACTION_SEND: Users want to share data through another app.

You can do this through the intent classsetAction()方法或intent构造方法来设置action属性。

If you want to define an action property string, don't forget to prefix the package name as a string when you define it.

eg

1 static final String action_timetravel = "Com.example.action.TIMETRAVEL";

Data:

The URI class refers to the function of data that exercises MIME type data. The type of data provided is generally determined by the intent action.

In addition to URIs, it is important to specify the data type (MIME type) when creating the intent class.

Set the URI type, using thesetData()方法,设置MIME类型,用setType()方法。两种都设置用setDataAndType()方法.(intent类)

Category:

Adds additional information to intent as a string.

Most intent do not need to add category.

CATEGORY_LAUNCHER: Activity is an initial task activity and is listed on the application manager.

CATEGORY_BROWSABLE: Target activity allows the browser to launch a reference link to display the data

addCategory()Add Category Property by method

Extras:

Save additional information in the form of key-value pairs to complete the required functions, as if some actions correspond to specific data URIs, and some actions correspond to specific extras.

putExtra()add extras by method.

A custom extra string.

1 static final String Extra_gigawatts = "Com.example.EXTRA_GIGAWATTS";

Flags:

Defines how to start the activity and how to treat him.

Translated from the Android website API guide.

Android API Guides Learn--intent and Intent Filters

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.