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

Source: Internet
Author: User

Android learning notes-Intents and Intent Filters (1), 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/3991792.html

  • Knowledge Point: definitions, functions, and usage of Intents and Intent Filters.

  • Intent
    Intent refers to "Intent". As it means, it is expressed in Android.Intention to implement a function(Intention to do an action ).

Definition:

Intent is the messaging object that transmits messages that require certain functions of other application components. The scope of Intent can be

In the same Android Application, it can also be different applications. For example, we often use the scan function to enable the system to take photos of the Activity.

Intent is an android. content. Intent object, which can carry data through Bundle.

Purpose:

1. start an activity)

1 // start method: 2 3 startActivity (Intent intent) 4 5 startActivityForResult (Intent intent)

 

2. start a service)

1 // start method: 2 3 startService (Intent service) 4 bindService (Intent service, ServiceConnection conn, int flags)

 

3. Release broadcast (deliver a broadcast)

// Start method sendBroadcast (Intent intent) sendBroadcast (Intent intent, String comment) sendOrderedBroadcast (Intent intent, String comment) sendOrderedBroadcast (Intent intent, String comment, handle comment, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) sendStickyBroadcast (Intent intent) broadcast (Intent intent, BroadcastReceiver handle, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)

Type

Intent can be divided into two types:Show Intent(Explicit intents) andImplicit Intent(Implicit intents)

Show Intent:

Specify to start the component by name (fully qualified class name the fully-qualified class name. Generally, we use clear Intent in our own app to start the component, because we know the class name of activity or service in our app.

For example:

As feedback on user operations, you can enable a new activity or a service for downloading files in the background.

Implicit Intent:

The class name of a specific component is not required. Instead, an Intent with a general function is declared, which will be processed by other application components.

 

 

When we create a display Intent to enable an activity or service, the system will immediately enable the application components described in detail in the Intent object.

1 // explicitly Intent2 Intent I = new Intent (MainActivity. this, SecondActivity. class); 3 startActivity (I );

 

When we create an implicit Intent, the Android system compares the intent content in the manifest file with the intent filter (filters) declared by other apps on the device, to find the appropriate components to enable. If the intent matches (match) the intent filter, the system enables the component and transmits the intent object. If multiple intent filters match, the system displays a dialog box for the user to select an app.

1 // implicit Intent2 Intent intent = new Intent ("android. intent. action. DIAL"); 3 startActivity (intent );
1 <! -- Intent filter declared in the Manifest file --> 2 <activity3 android: name = "SecondActivity"> 4 <intent-filter> 5 <action android: name = "android. intent. action. DIAL "/> 6 <category android: name =" android. intent. category. DEFAULT "/> 7 </intent-filter> 8 </activity>

 

 

1. Activity A creates an Intent that describes the function and passes it to startActivity ()

2. The Android system searches for intent filters of all apps to match the previous intent.

3. When a conformity is found, the system enables the conformity Activity (Activity B) and passes the Intent to it by calling its onCreate () method.

 

How can we better understand these two intents?

Here is an example in crazy Android handout to help us understand what explicit Intent and implicit Intent are, for example. It's like a girl has the intention of finding a boyfriend and expresses her intention in two ways.

First, she clearly pointed out that, to find "Wu yanzu" as a boyfriend, this explicit way of specifying to find someone is like an explicit Intent.

Second, she pointed out that it is not important for her to be a boyfriend in high, rich, and handsome status. As long as she meets these three conditions, this method is like implicit Intent.

 

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.

Intent problems in android

The Android development website explains that if you want to allow other apps to hit your app through intent-filter, we need to add default category to our app, generally, no default category indicates that this activity must be used only by our app. For example, if one app has multiple activities and only the main activity can access other activities, this is the case. Because the implicit method of starting an app is by comparing intent conditions, if we do not specify the default category, it means that we do not intend to be started implicitly by other programs (for example, we want to release a trojan or something ).

This document explains that to start an activity implicitly, you need to add default category. This is because you need to allow other apps to start your activity (and you can even use the class name to start your activity directly, ).

In addition, when we want to column the main control activity in the Application List, we will add launcher category to it.

For example, for a product management program, the main control activity first opens the list of currently popular products, which is in the Application List of mobile phones. Therefore, a launcher category is required, it has a product-specific activity that allows you to view the product by using a product number, or even a link on the webpage, this product details the activity and requires a default category, but does not require launcher category. The other activity that modifies the product information does not have to add a category, because it will only be started by the master activity, and other external apps should not have the opportunity to start it implicitly.

Developer.android.com/..s.html

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.