Intent Introduction
Android provides the intent mechanism to assist the interaction and communication between applications, intent is responsible for the action of one operation in the application, the action involves data, the additional data description, Android according to this intent description, is responsible for finding the corresponding component, will Intent is passed to the calling component and completes the call to the component. Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent plays a role as a media intermediary, specifically providing information about the components that are called to each other, and decoupling the caller from the callee. The intent function is shown in the SDK as follows:
- by Context.startactivity () Oractivity.startactivityforresult ()
Initiate an activity;
- Start a service through Context.startservice (), or interact with Context.bindservice () and background services;
By broadcasting methods (such as Context.sendbroadcast (), Context.sendorderedbroadcast (),
Context.sendstickybroadcast ()) issued to broadcast receivers.
Intent can be divided into implicit (implicitly) and explicit (explicitly) two types:
(1) Display Intent
That is, when constructing the intent object, the receiver is specified, and it is generally implemented within the same application with the knowledge of the target component name, as follows:
new Intent(MainActivit.this, NewActivity.class);startActivity(intent );
In the intent above, the recipient is directly indicated: newactivity
(2) Implicit Intent
That is, when the sender of the intent constructs the intent object, it does not know or care who the receiver is, it is helpful to reduce the coupling between sender and receiver, it is generally used in the premise that the target component name is not explicitly stated, and it is usually applied between different applications, as follows:
new Intent();intent.setAction("com.wooyun.test");startActivity(intent);
The intent above does not indicate the recipient, but gives an action as a filter for the receiver.
For explicit intent,android does not need to parse, because the target component is already very clear, Android needs to parse the implicit intent, through parsing, intent mapping to the activity that can handle this intent, Intentreceiver or service.
Intent Filter Matching rules
The intent parsing mechanism is primarily to find a matching intent by locating all intentfilter in the androidmanifest.xml and the intent defined therein. During this parsing process, Android is judged by the three attributes of the action, type, and category of intent. There can be more than one action, type, category in a filter list, and all action, type, category categories, and the same category of information together constrain the matching process for the current category. Only one intent match the three categories of action, type, category to match exactly, only an exact match to initiate activity. If another component declares more than one intent Filter, it can only be started by matching any one of the components.
For example:
<action android:name="Com.wooyun.project.SHOW_CURRENT" /><category android:name="Android.intent.category.DEFAULT" /><data android:mimetype="Video/mpeg" android:scheme="http" . . . /><data android:mimetype="image/*" /><data android:scheme="http" android:type= "video/*" />
(1) Match rules for action
The action is a string, and if intent indicates the action is specified, the action must be included in the Intentfilter action list of the target component, otherwise it cannot be matched. A intent filter can declare that the action in more than one action,intent is identical to any of the actions in the string form (note that case-sensitive, case-insensitive, but the same string content will cause a match to fail), The action facet is successful. You can set the action for intent by using the Setaction method, or you can pass in the action when constructing intent. It is important to note that an implicit intent must specify an action. For example, we defined the following intent Filter for myactivity in the manifest file:
<intent-filter> <action android:name="android.intent.action.SEND"/> <action android:name="android.intent.action.SEND_TO"/></intent-filter>
So as long as the intent action is "SEND" or "send_to", then this intent will be able to match the action with the above activity successfully. For example, our intent definition is as follows:
new Intent("android.intent.action.SEND") ;startActivity(intent);
Then our intent is matched with myactivity in action.
The Android system pre-defines a number of actions, which represent some common operations. The common action is as follows (constants in the intent Class):
Intent.ACTION_VIEWIntent.ACTION_DIALIntent.ACTION_SENDTOIntent.ACTION_SENDIntent.ACTION_WEB_SEARCH
(2) Data matching rules
If the intent does not provide a type, the system will get the data type from it. As with action, as long as intent's data is exactly the same as any of the data declarations in the intent filter, the data aspect is successfully matched.
Data is made up of two parts : MimeType and URI
Minetype refers to media types such as IMGAGE/JPEG,AUTO/MPEG4 and viedo/*, which can represent different media formats such as pictures, text, video, etc.
The URI is provided by scheme, host, port, path | Pathpattern | Pathprefix These 4 parts are composed
<scheme>://:<port>/[<path>|<pathPrefix>|<pathPattern >]
For example:
Content://com.wooyun.org:200/folder/etc
Http://www.wooyun.org:80/search/info
The URI of the intent can be set by the SetData method, MimeType can be set by the Settype method.
It is important to note that if a URI is not specified in the Data Declaration section of Intent filter, the scheme portion of the URI in content or file,intent must be a content or file to match the default URI To specify the complete data for the intent, the Setdataandtype method must be used for the reason that we find in the source code of SetData and Settype methods:
publicsetData(Uri data) { mData = data; null; returnthis;}
publictype) { null; type; return this;}
These two methods will clear each other's value (this is more amusing), that is, SetData will put MimeType to Null,settype will set the URI null.
Let's illustrate the match of data. First, let's take a look at the syntax for specifying data in intent filter:
< Data android:scheme= "string." android:host= " string " android:port= " string "android:path=" string "android:pathpattern=" string "android:pathprefix=" string "android:mimetype=" string /> where the scheme, host, and other parts need not be specified.
Use case:
(1) If we want to match HTTP with ". pdf" to the end of the path, so that other programs want to open the network PDF, users can choose our program to download view.
We can set scheme to "HTTP", Pathpattern set to ". *//.pdf", and the entire intent-filter is set to:
<intent-filter> <action android:name="Android.intent.action.VIEW"></Action> <category android:name="Android.intent.category.DEFAULT"></category> <data android:scheme="http" android:pathpattern=". *//.pdf" ></Data> </intent-filter>
If you only want to work with a PDF of a site, adding android:host= "yoursite.com" to the Data tab will only match http://yoursite.com/xxx/xxx.pdf, but that won't match Www.yoursite.com, if you also want to match this site, you will need to add a data tag, in addition to Android:host instead of "www.yoursite.com" the others are the same.
(2) If we are doing an IM application, or other applications like Weibo, how can someone else make a call through Intent in the selection box? We only use registered Android.intent.action.SEND and MimeType as "Text/plain" or "/" on it, the whole intent-filter is set to:
<intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data mimeType="*/*" /> </intent-filter>
The reason for setting the category here is that the default category for the created instance of Intent contains the Intent.category_default, which is why Google does this so that the Intent always has a category.
(3) If we are doing a music playback software, when the file browser opens a music file, so that our app can appear in the selection box? This is similar to the file association, actually do it as above, also very simple, we only use registered Android.intent.action.VIEW and mimeType for "audio/*" on it, the whole intent-filter set to:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="audio/*" /> </intent-filter>
(3) Match rules for category
The category is also a string, but it differs from the action's filtering rule, which requires that if a category is included in the intent, all the category must be the same category as the filter rule. In other words, if a category is present in the intent, there are several category, and for each category it must be the category defined in the filter rule. Of course, there can be no category in intent (if the category is not specified in intent, the system will automatically bring "Android.intent.category.DEFAULT"), if not, it can still match successfully. The difference between category and action is that action requires an action in intent and must be the same as a certain action in the filter rule, and the category requires intent to have no category. But once the category is found, no matter how much you have, each one should be able to be the same as any category in the filter rule. We can add category to intent by Addcategory method.
Special Note:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /></intent-filter>
The two appear together, indicating that the activity is an entry activity and that it appears in the list of system applications, both of which are indispensable.
Intent Filter FAQs Summary (1) The difference between path, Pathprefix, Pathpattern
Path is used to match the complete path, such as: http://example.com/blog/abc.html, where path is set to/blog/abc.html to match;
The pathprefix is used to match the beginning of the path, and the Uri to take it, the Pathprefix is set to/blog to match;
Pathpattern matches the entire path with an expression, which requires the matching symbol and escape.
Match symbol:
"" is used to match 0 times or more, such as: "A" can match "a", "AA", "AAA" ...
"." to match any character, such as: "." Can Match "a", "B", "C" ...
So ". *" is used to match any character 0 or more, such as: ". *html" can match "abchtml", "cHTML", "html", "sdf.html" ...
Escape: Because "/" is treated as an escape character when reading XML (before it is used as a pathpattern escape), so it needs to be escaped two times, read Xml once, and used again in Pathpattern. such as: "" This character should be written "//", "/" This character should be written as "////".
(2) query if there is activity to match the component we specified intent
Using Packagemanager's resolveactivity or intent Resolveactivity method will get the most suitable activity for intent.
Calling Packagemanager's queryintentactivities will return all activity that successfully matched intent
(3) The difference between Android.intent.action.MAIN and Android.intent.category.LAUNCHER
Difference One:
Android.intent.action.MAIN decide that an application starts the component first
Android.intent.category.LAUNCHER determines whether the application is displayed in the program list (it is plain whether an icon is displayed on the desktop)
The combination of these two attributes:
first case : There is main, no launcher, no icon in the program list
Cause: Android.intent.category.LAUNCHER Determines whether the application appears in the program list
second case : No main, launcher, no icon in the program list
Reason: Android.intent.action.MAIN determines the activity that the application starts first, if there is no main, then does not know which activity to start, therefore does not have the icon to appear
So these two properties generally appear in pairs.
If there are two components in an application intent-filter have added Android.intent.action.MAIN and
Android.intent.category.LAUNCHER these two properties, the app will display two icons, written in front of the component to run first.
Difference Two:
Android.intent.category.LAUNCHER:android.intent.category.LAUNCHER determines whether the application is displayed in the list of programs, which is the main program list after Android is powered on.
Android.intent.category.HOME: Press and hold "Home" button, the program is displayed in the home list.
(4) About implicit intent
Each implicit Intent that is emitted through the startactivity () method has at least one category, which is "Android.intent.category.DEFAULT", so as long as you want to receive an implicit Intent The Activity should include the "Android.intent.category.DEFAULT" category, otherwise it will cause the intent match to fail.
For example, if an activity component wants to be called by another component through an implicit intent, its declaration in manifest.xml is as follows:
<activity android:name="com.wooyun.org.MainActivity"> <intent-filter> <action android:name="com.google.test" /> <category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
(5) Match priority on Intent-filter
First look at the intent filter (Intent-filter) and look for the following precedence relationship: Action->data->category
Reference Link: http://blog.csdn.net/cnnumen/article/details/8464786
Android Development Advanced Technology QQ Group: 108721298 welcome into the group
Public Number: Mobilesafehome
(This public number supports voting)
You have to figure it out. Intent Filter Match rule