Android--intent and Intent Filters

Source: Internet
Author: User
Tags home screen

Http://www.cnblogs.com/XP-Lee/p/3613830.html

The intent is a message object that activates the component for communication between components. It is important to note that there are only three types of components that can be activated by intent: Activity, service, and Broadcastreceiver. There are three usage scenarios for these three types of components Intent :

    • Start activity: In the app, an activity represents a user interface. To Intent start an activity instance by calling StartActivity () for the parameter. This intent告诉Activity去启动,并且传递了一些必要数据给它。在Activity结束时,如果想要从这个结束的Activity接收数据,可以使用startActivityForResult()启动,这样在你的Activity的 onActivityResult()回调就会接收到单独的结果Intent。
    • Startup Service:service does not have a user interface, its responsibility is to perform operations in the background. To Intent startService() Start an activity instance for a parameter call. This intent告诉Service去启动,并且传递了一些必要数据给它。如果Service被设计成C/S结构的Server,你可以在其它组件中以Intent为参数调用bindService()绑定这个Service。
    • Send broadcast: A broadcast message can be received by any app. The system sends broadcasts for various system events, and you can also send broadcasts ( sendBroadcast() , sendOrderedBroadcast() , or sendStickyBroadcast() ).

These intent pass, and there will be no overlap. The broadcast intent is passed only to receiver, never to the activity or service, and a intent to the activity is delivered only to the activity, never to a service or receiver. ; and so on.

Intent type (Intent Types)

There are two types of intent:

    • Explicit intent: Starts a component with a full-class name. The Display intent boot component is in your own app, because you need to know the class name of a component.
    • Implicit intent: Not a practical class name, but an action to start. The implicitly intent-initiated component allows the application to come from another app.

With an explicit intent boot, the Android system can start the component immediately through this intent object.

When the implicit intent is started, the Android system will compare the intent content with the intent filter in the manifest file of all apps in the device to find the most appropriate component to start. If the intent and intent filters match, the system starts the component and passes the intent object. If more than one intent filter is matched, a dialog box is displayed for the user to choose which app to use. (Note that the INTETN filter is a symbol in the manifest that indicates what kind of intent a component can receive.) )

The process of initiating activity for an implicit intent. "1" Activity a creates a intent through action, allowing the intent to call StartActivity () after the argument. The "2" Android system is implicitly intent, searching for intent filtering that matches all apps. The "3" system matches to activity B, calls its OnCreate () method, and passes the intent.

Warning: To ensure the security of your app, you typically start the service with an explicit intent and do not declare the INTETN filter for the service. Starting the service with an implicit intent is dangerous because you cannot determine how many service will respond to this intetn, and the user cannot see which service is started.

Create Intent (Building an Intent)

The main information of intent is as follows:

Component Name

The name of the component to start.

For implicit startup, the Component name is optional. However, for display startup, Component name is the necessary information. A intent that has no component name information is an implicit intent, and the system determines which component to receive intent based on other information from intent (such as action, data, category).

Action

A string that performs a common operation.

The system framework provides a number of action constants, which list some of the following:

Constant Target component Action
Action_call Activity Call
action_screen_on Broadcastreceiver Screen Open
Action_timezone_changed Broadcastreceiver Time zone changes

To customize the action, you should use the package name as a prefix. Like what:

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

The URI of the data being executed and its MIME type. Typically, different actions are accompanied by different data types. For example, if the action is Action_edit, then data will contain the URI to be edited. If action is action_call, data will be the URI for Tel: phone number. If the action is Action_view, then data is http: The URI of the network address.

When creating a intent, it is necessary to specify a MIME type for the data as a complement to the URI. For example, an activity can display a picture but cannot play audio (although the URI of the picture and audio is very similar). Therefore, specifying a MIME type for the data can help the Android system find the best component to receive intent.

However, MIME types can sometimes be inferred from special URIs. such as content: a URI, which indicates that the data is located above the device, and that the data isContentProvider控制的,这就使的数据的MIME类型对系统来说是可见的。

Warning: If you want to set the URI and MIME type, you should call it instead setDataAndType() of calling both SetData () and Settype (), because they invalidate each other's values.

Category

A string of additional information that contains information about the types of components that can handle the intent. A intent can have multiple category, but most intent require category. Here are some public category information:

Constant Meaning
Category_browsable The target activity can be activated by the browser to display the data of the linked application-for example, an image or an email
Category_gadget This activity can be embedded into another activity.
Category_home The Activity TV home interface, which is the first page that the device opens or presses the home button to see
Category_launcher This activity can be used as the initial activity of a task and is included in the Application Launcher
Category_preference Target activity is an options panel

The above content (component name, action, data, and category) describes the typical characteristics of intent. By reading this content, the Android system can decide which component can be started.

However, intent can also carry some additional information that does not affect the system selection component, which is the extras and flags.

Extras

Additional information in the form of Key-value.

The intent class defines a number of EXTRA_* standard data types for constants. If you want to customize extra keys, you should use the package name as a prefix:

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

Flag is defined in the intent class as the metadata of the intent (data describing the data). Flag can tell Android how to start an activity (for example, which task the activity belongs to) and how to manage the activity after it is started (for example, whether the activity belongs to recent Activities list).

Using the pending Intent (using a Pending Intent)

Pendingintent is the packaging of intent. The main purpose of Pendingintent is to grant external programs the use of packaged intent, as if intent were executed in their own process.

The main use scenarios for pending intent are as follows:

    • The user performs one of your notification related actions (the Android system'sNotificationManager会执行这个Intent)。
    • The user performs an action related to your app widget (the Home screen app will perform this intent).
    • A certain time in the future (Android Alarmmanager will perform this intent).

Because intent is designed to handle only certain types of components (Activity, service, and Broadcastreceiver), the creation of Pendingintent must also be based on this layer of consideration. When you create pendingintent, you must call the corresponding method creation, depending on the component type:

    • PendingIntent.getActivity()For a that Intent starts an Activity .
    • PendingIntent.getService()For the that Intent starts a Service .
    • PendingIntent.getBroadcast()For a, that Intent starts an BroadcastReceiver .

These methods are best used in the context of the current app. For more information on the use of Pendingintent, refer to the API guide for notifications and app widgets.

Intent parsing (Intent Resolution)

When the system receives an implicit intent, the system looks for the best activity by comparing the intent and intent comparators, and the system is compared based on the following three aspects:

    • Action
    • Data (URI and datatype--mime)
    • Category
Action Test

A intent filter can declare 0 or more <action> elements. Like what:

1 <intent-filter>2     <action android:name= "Android.intent.action.EDIT"/>3     <action android: Name= "Android.intent.action.VIEW"/>4 ...     5 </intent-filter>

Android--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.