Android Using Intent application

Android Using Intent application

Seemingly ordinary most strange, as hard as it is easy. Northern Song Dynasty. Wang Anshi
Actually, the most seemingly ordinary things extraordinary, simply can not do a good job; success seem very easy, and the success of the process was fraught with hardships.
For we believe that a very common thing, dismissive, would never grow in, down to earth, just from the success and step forward; success does not like to see so easy to find a shortcut is not desirable, we often than others pay more hard work and effort.
Today we are speaking about the principles and applications of the Android Intent.
Earlier we summarize a few important components of Android, I believe we have been for these components have a clear understanding, we look at a few common operations:
Start a Activity: Context.startActivity (Intent intent);
Start a Service: Context.startService (Intent service);
Binding a Service: Context.bindService (Intent service, ServiceConnection conn, int flags);
Send a Broadcast: Context.sendBroadcast (Intent intent);
We found that in these operations, there is an Intent to participate, looks like a very important component, so in the end Intent What is it?
In simple terms, Intent is for data transfer between the various components of the system's data load. When we need to make a call to action, we can tell by Intent Android system to complete this process, Intent is to call an operation notification.
Intent has several important attributes, here we will be introduced one by one:
1.action, actions to be executed
For the following statement of Activity:



TargetActivity stated in its <intent-filter> in the <action>, ie, the target action, if we need to make a jump action, you need to specify the target in the Intent of the action, as follows:



When we specify the appropriate action for the Intent, then call startActivity method, the system will jump to the corresponding Activity according to action.
In addition to the action custom, Intent also contains a number of default action, just to name a few:



Every action has its specific purpose, the following will use to them.
Additional information 2.data and extras, that is to be operated to perform data movement and delivery to destination
Here's give an example to interact with the browser:


The above two methods are the start the browser and open the specified web page, keyword search, corresponding to the action is Intent.ACTION_VIEW and Intent.ACTION_WEB_SEARCH, the former need to specify the page address, which is required to specify the key information for critical word search, the browser will follow the default search engine set up their own search.
We note that when you open the page, specify a data attribute of Intent, which is actually designated to operate the data is in the form of a URI, we can convert a specified string prefix to a specific URI types, such as: " http: "or" https: "represents the network address type," tel: "indicates the phone number type," mailto: "indicates the type of e-mail addresses, and so on. For example, we want to call a given number, you can do this:



So how do we know whether or not to accept such a target prefix it? This requires a look at the matching rules target <data /> element of the.
Target <data /> tag contains the following sub-elements, they define the url matching rules:
android: scheme  matches the url prefix, in addition to the "http", "https", "tel" ..., we can define your own prefix
android: host  url match the hostname portion, such as "google.com", if defined as "*" indicates that any host name
android: port  to match the port url
android: path  matches the url path
We change what TargetActivity Footnotes:



If you specify only action at this time is not enough, we need to set the data value, as follows:



At this time, url and TargetActivity each part of all configuration information is consistent jumps succeed, otherwise it is rejected by the system.
We android: path = "/ target" is amended as android: pathPrefix = "/ target", and to meet the above requirements.
And during the search, we use a putExtra method, the keyword is placed as a parameter Intent, we become extras (additional information), there involves a Bundle object.
Bundle and Intent has a close relationship, it is mainly responsible for the Intent to save additional parameter information, which implements the android.os.Paracelable interface internally maintains a Map property type, with key pairs used to store additional parameters. When placing additional information in our use of putExtra Intent, the method checks the default Bundle examples is not empty, if empty, then create a new Bundle instance, and then place the specific parameter information to Bundle instance. We can also create your own Bundle object, you can then specify the Bundle of Intent, as follows:



It should be noted that, after using the method set putExtras Bundle object, not a reference to the operating system, but the copy operation, and then change the data so bundle instance after you have finished setting will not affect the interior of Intent additional information. How do we get the additional information provided in the Intent of it? Corresponding is, we need to get to the Bundle instance of Intent, and then removed from the key information corresponding to:



Of course, we can also use the Intent of getIntExtra and getStringExtra method to obtain, the data sources are in the Bundle Intent instance of an object type.
Earlier we relate to the three attributes of Intent: action, data and extras. In addition, Intent also includes the following properties:
3.category, to implementation of the objectives of the action or behavior traits have classified
For example: there are usually configured in our application main interface Activity:



Activity on behalf of the target task is the application resides in the initial Activity and the list appears in the application launcher in the system.
Several common category is as follows:
Intent.CATEGORY_DEFAULT (android.intent.category.DEFAULT) default category
Intent.CATEGORY_PREFERENCE (android.intent.category.PREFERENCE) indicates that the target Activity is a preference interface;
Intent.CATEGORY_BROWSABLE (android.intent.category.BROWSABLE) specified after this category, click on the image on the page or link, the system will consider the inclusion of this objective Activity optional list for the user to choose to open a picture or a link.
When setting the category of Intent, you should use addCategory (String category) method to add categories of information specified in the Intent to match the declared goal of Activity in this category.
4.type: To perform a MIME data type of the target actions can handle Activity
For example: a picture of the target can be handled include such mimeType Activity in its statement:


When using Intent match, we can use setType (String type) or setDataAndType (Uri data, String type) to set mimeType.
5.component, package or class name of the target component
When using the component matching, generally use the following forms:




Among them, the first two are used to match the target in the same package, and the third is used to match the objectives of the other package. It should be noted that, if we specify component properties in the Intent, the system will no longer be on the action, data / type, category match.

Share this

Related Posts

Previous
Next Post »