Intent Usage Summary in Android development _android

Source: Internet
Author: User
Tags sqlite database

This example describes the intent usage in Android development. Share to everyone for your reference, specific as follows:

Android phone software development, intent as a mobile phone software development is very important objects need to arouse our attention, in fact, intent is also the embodiment of Android Development has its uniqueness of a landmark object.

When an activity is to start another activity, perhaps a previously familiar pattern is to call a new function, create an object that has a window feature class directly, or call a startup function directly to start. This approach is simple and straightforward, but it violates the idea of Android development. Android uses intent to "encapsulate" the "invoke intent" of a program, regardless of what component the program wants to start, start an activity, a service, or a broadcast receiver, Android uses the intent object to encapsulate this "launch intent".

In addition, there are benefits to using intent, where applications simply want to start components or applications with certain features, but do not want to have rigid code coupling with such programs, and the application may sometimes just know some of the features of something to be started, It is not known what program to start specifically refers to (for example, a program wants to start texting applications, and at this time there are multiple messages in the system software, we do not know what to open software, at this time the program should be to send a Start message application intent, rather than specify what software to open. Of course, more generally, intent can also specify the application to open, and some programs have a rigid coupling relationship.

Intent is also an important medium for communication between application components, where two activity encapsulates data that needs to be exchanged into bundle objects and then uses intent to carry bundle objects, thus achieving data exchange between two activity.

Intent has several properties: Component,action,category,data,type,extra,flag seven properties, where the Component property is used to specify the target component that needs to be started. The extra property is used to carry data that needs to be exchanged .

The following is a detailed description of the intent object:

1. Component Properties of Intent

component is used to specify the properties of the startup target component, and the standard boot code is as follows:

ComponentName comp = new ComponentName (firstactivity.this,secondactivity.class);
Intent Intent = new Intent ();
Intent. SetComponent (comp);
StartActivity (Intent);

The code above is used to create the ComponentName object in the standard, and then invoke the SetComponent () function of the intent object to set the corresponding componentname for intent. Finally, call the Startacitvity function to start a new activity.

In fact, when you need to set the component attribute for intent, intent has provided us with a simple constructor, using the following method (the way we often use it):

Copy Code code as follows:
Intent Intent = new Intent (firstactivity.this,senondactivity.class);

It is worth mentioning that, in the secondactivity component, you can use the Getintent () method to get the intent object that calls the secondary component, and then the GetXXX method can be used in a variety of ways.

2. Intent Action and Category attributes

Both the action and category properties are ordinary strings, where the action represents the abstract "action", and the category attribute is used in conjunction with the action attribute to express intent to start a component.

Activity with <intent-filter.../> tags is likely to be activated.

Like what:

Public final staticstring some_action = "Org.someaction.SOME_ACTION"
//This is a character turn, casually set, but generally have some abstract semantics.
in the onclick () method of a button in this activity species, add:
Intent Intent = Newintent ();
Intent.setaction (thisactivity.some_action);
StartActivity (Intent);

Such code does not specify which activity to start, so it is out of "hard coding," but which activity is to be started, depending on the <intent-filter.../> tag in the activity configuration file.

<intent-filter.../> is the child element of the <activity.../> element in the Androidmanifest.xml file, and what needs to be done is to give you an activity that actually requires the corresponding intent to add < Intent-filter.../> tags, in <intent-filter.../>, there are three kinds of labels:1.<action.../>2.<category.../>3.<data.../> , where the Android:name attribute is specified, and the activity has the attribute of the corresponding intent.

For the above intent, add such code (under <intent-filter.../> tags, of course):

Copy Code code as follows:
<action android:name = "Org.someaction.SOME_ACTION"/>

One thing to mention is that a intent object can contain at most one activity attribute, and the program calls Setaction (STRINGSTR) to set the property value of the action, and an activity can have multiple category properties. The program can invoke Addcategory (String str) to add the Category property. When a program creates a intent, the intent property that is created automatically starts with a Intent.category_default constant whose value is "Android.intent.category.DEFAULT", so <categoryandroid:name = "Andrid.intent.category.DEFAULT" > can be added to a configuration file when an activity attribute is configured.

In fact, Android provides a number of standard action and category constants internally.

Summarized as follows:

Action constants

The corresponding Android:name setting

Simple description

Action_main

Android.intent.action.MAIN

Application Portal

Action_view

Android.intent.action.VIEW

Show specified data

Action_attach_data

Android.intent.action.ATTACH_DATA

Specifies where a module's data is appended

Action_edit

Android.intent.action.EDIT

Edit specified data

Action_pick

Android.intent.action.PICK

Select an item from the list and return the selected data

Action_chooser

Android.intent.action.CHOOSER

Show an activity selector

Action_get_content

Android.intent.action.GET_CONTENT

Let the user select the data and return the selected

Action_dial

Android.intent.action.DIAL

Show Dial panel

Action_send

Android.intent.action.SEND

Send data directly

Action_sendto

Android.intent.action.SENDTO

Send a message directly

Action_answer

Android.intent.action.ANSWER

Answer phone

Action_insert

Android.intent.action.INSERT

Inserting data

Action_delete

Android.intent.action.DELETE

Delete data

Action_run

Android.intent.action.RUN

Running data

Action_sync

Android.intent.action.SYNC

Performing data synchronization

Action_pick_activity

Android.intent.action.PICK_ACTIVITY

For selecting an activity

Action_search

Android.intent.action.SEARCH

Perform a search

Action_web_search

Android.intent.action. Web_search

Diameter web Search

Action_ Factory_test

Android.intent.action.FACTORY_TEST

Factory Test Entry point

Just a few excerpts, if you want to know all the action, you can see the Android standard API for the intent section.

3. Data and type properties in intent

Use a few examples to illustrate the use of data:

Add the following code to the onclick () method of a button:

String data = http://3g.renren.com;
URI URI =uri.parse (data);
Intent.setaction (Intent.action_view);
Intent.setdata (URI);
StartActivity (Intent);

This method will make the button start Renren.

Of course, the code is simple:

Uri Myuri = Uri.parse ("http://3g.renren.com");
Intent Intent = new Intent (Intent.action_view,myuri);
StartActivity (Intent);

The above is a more detailed set of methods.

4. Extra properties of Intent

The intent property is typically used to exchange data between multiple activity, and the intent extra attribute value should be a bundle object, and he can enter multiple key-value pairs, This allows for a corresponding exchange of data between different activity through intent.

Intent offers a number of methods as follows:

Putextra (Bundledata) Getextras ()
Putxxx (stringkey XXX data) getxxx (String key)
Putserializable (Stringkey, Serializable data) corresponds to a
Getserializable (Stringkey, Serializable data)

In the courseware has given the add the key value pair the correlation code, here does not repeat.

Summarize:

Android apps always use intent to implement a component that needs to be started, intent is the encapsulation of this "startup intent", which is not coupled to any program component, which provides a good way to provide scalability and maintainability of the program, where < The intent-filter/> configuration is the most important label of the program component, it is best to use Eclipse to add the corresponding label, because some small errors such as space often lead to some anomalies, I hope that children's shoes in the use of attention.

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Programming activity Operating Skills Summary", "Android Resource Operation skills Summary", "Android File operating skills summary" , "Android Operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card operation method Summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.