Intent components in Android

Source: Internet
Author: User

Intent is the communication between different components, encapsulating the conditions of communication between different components. Intent itself is defined as a category (class), a Intent object that expresses a purpose (Goal) or expectation (expectation), describes the service or action it expects, the data associated with the action, and so on. Android, based on this intent object, is responsible for pairing, finding the matching component, and then passing the intent object to the found component, and the Android Matchmaker task is complete.

 

This describes intent in Google Doc (from the Android Chinese translation group)
when a request is received from the Contentresolver, the content provider is activated. The other three components ──activity, service, and broadcast receivers are activated by an asynchronous message called intent. Intent is a intent object that holds the content of the message. For activity and service, it indicates the requested action name, as well as the URI and other information that is the data for the action object. For example, it can host a request for an activity, let it display a picture for the user, or let the user edit some text. For broadcast receivers, the intent object indicates the declared behavior. For example, it can declare the camera button to be pressed on all objects of interest.

for each component, the method of activation is different:
1. An activity by passing a intent object to context.startactivity () or Activity.startactivityforresult () to load (or designate a new job). The corresponding activity can view the intent that activates it by calling the Getintent () method. Android invokes the activity's Onnewintent () method to pass it to its secondary intent.

an activity often starts the next one. If it expects the activity it initiates to return a result, it will replace startactivity () with the call to Startactivityforresult (). For example, if it launches another activity to allow the user to pick a photo, it may want to know which photo is selected. The result is encapsulated in a intent object and passed to the Onactivityresult () method of the activity that made the call.

2. By passing a intent object to Context.startservice () a service is started (or given a new instruction to the running service). Android invokes the OnStart () method of the service and passes the intent object to it.

Similarly, a intent can be passed to Context.bindservice () by the calling component to get a connection to a running target service. This service gets the intent object through a call to the Onbind () method (if the service has not been started, Bindservice () starts it first). For example, an activity can connect to the aforementioned music playback service and provide the user with an actionable (user interface) to control playback. The activity can call Bindservice () to establish a connection and then invoke the object defined in the service to affect playback.

3. The application can pass intent objects to Context.sendbroadcast (), Context.sendorderedbroadcast (), and Context.sendstickybroadcast () and other similar methods to generate a broadcast. Android invokes the OnReceive () method of all broadcast receivers that are interested in this broadcast to pass intent to them.

What the intent object contains

variables for intent related content are defined in the Java source code of the intent class, as follows:

Action  private String maction;  Data  private Uri mdata;  Private String mtype;  Private String mpackage;  ComponentName  private componentname mcomponent;  Flag  private int mflags;  Category  private hashset<string> mcategories;  Extras  private Bundle Mextras;

1.componentname(component name), specifies the class name of the target component of the intent. The component name is optional, and if filled, the intent object is sent to the component with the specified component name, otherwise it can be positioned to the appropriate component through other intent information. The component name is an object of type ComponentName.
Usage:


Intent Intent = new Intent ();      The constructed parameter is the classpath name of the current context and target component      componentname cn = New ComponentName (Helloactivity.this, " Com.byread.activity.OtherActivity ");      Intent.setcomponent (CN);      StartActivity (Intent);




equivalent to the following common methods:
Intent Intent = new Intent ();      Intent.setclass (Helloactivity.this, otheractivity.class);      StartActivity (Intent);




The Intent class also contains a constructor that initializes the ComponentName:

Public Intent (Context Packagecontext, class<?> cls) {          mcomponent = new ComponentName (Packagecontext, CLS);      }




2.Action (action), specifies the execution action of the intent, such as calling the call component.

Public Intent (String action) {          maction = action;      }



3.data, which plays a role in representing data and data MIME types. Different actions are matched to different data types, and are obtained by setting the URI of the data.

Public Intent (String action, Uri uri) {      maction = action;      Mdata = URI;  }




such as calling the call component:

Uri uri = uri.parse ("tel:10086");  The arguments are the  Intent Intent = new Intent (intent.action_dial, Uri)  of the ACTION to call the telephony component and the URI to get data. StartActivity (Intent);




4.Category (Class), additional information about the action being performed. For example, the app's startup activity sets the category in Intent-filter.

<intent-filter>          <action android:name= "Android.intent.action.MAIN"/>          <category android: Name= "Android.intent.category.LAUNCHER"/>      </intent-filter>




5.Extras(additional information) to provide additional information for processing the intent component. Information can be accessed through the PUTXX () and Getxx () methods, or by creating bundle objects and accessing them through the Putextras () and Getextras () methods.

6.flags, which indicates how Android starts the target activity, sets the method to call intent's SetFlags method. The commonly used flags parameters are:

Flag_activity_clear_top
Flag_activity_new_task
Flag_activity_no_history
Flag_activity_single_top

Delivery of Intent

1. an explicit way . Set the componentname of the target component directly for message delivery within an application, such as starting another activity or a services.
The componentname of the target component is established through intent's setcomponent and SetClass.

2. implicit mode . ComponentName is empty and is used to invoke components in other apps. Need to contain enough information so that the system can use intent filter to filter action, data, or category in all components to match the target component, based on this information. Refer to the Activity component in Android (5. Activity's Intent Filter)
If the action is specified by intent, the action must be included in the Intentfilter action list of the target component, otherwise it cannot be matched;
If the intent does not provide a type, the system will get the data type from it. As with action, the data type list of the target component must contain the intent data type, otherwise it will not match;
If the data in intent is not a content: type URI, and intent does not explicitly specify its type, the match will be based on scheme (such as http: or mailto:) of the data in intent. As above, Intent scheme must be in the scheme list of the target component;
If intent specifies one or more categories, they must all appear in the list of organized categories. For example, the intent contains two categories: Launcher_category and Alternative_category, and the parsed target component must contain at least these two categories.

Intent calling common system components

Call the browser Uri Webviewuri = Uri.parse ("Http://blog.csdn.net/zuolongsnail");    Intent Intent = new Intent (Intent.action_view, Webviewuri);  Call map Uri Mapuri = uri.parse ("geo:100,100");    Intent Intent = new Intent (Intent.action_view, Mapuri);  Play mp3 Uri Playuri = uri.parse ("File:///sdcard/test.mp3");  Intent Intent = new Intent (Intent.action_view, Playuri);    Intent.setdataandtype (Playuri, "Audio/mp3");  Call to dial the phone Uri Dialuri = Uri.parse ("tel:10086");  Intent Intent = new Intent (intent.action_dial, Dialuri);  Direct call, need to add permission <uses-permission id= "Android.permission.CALL_PHONE"/> Uri Calluri = Uri.parse ("tel:10086");    Intent Intent = new Intent (Intent.action_call, Calluri);  Call Send mail (here to pre-configure the system email, otherwise it is not the departure mail interface) Uri Emailuri = Uri.parse ("mailto:[email protected]");  Intent Intent = new Intent (intent.action_sendto, Emailuri);  Direct email Intent Intent = new Intent (intent.action_send);  String[] tos = {"[email protected]"}; String[] CCS = {"[EMAIL&NBSP;PROtected] "};  Intent.putextra (Intent.extra_email, TOS);  Intent.putextra (INTENT.EXTRA_CC, CCS);  Intent.putextra (Intent.extra_text, "the email TEXT");  Intent.putextra (Intent.extra_subject, "SUBJECT");  Intent.settype ("Text/plain");    Intent.createchooser (Intent, "Choose Email Client");  Texting Intent Intent = new Intent (Intent.action_view);  Intent.putextra ("Sms_body", "The SMS Text");  Intent.settype ("vnd.android-dir/mms-sms");  Direct texting Uri Smstouri = Uri.parse ("smsto:10086");  Intent Intent = new Intent (intent.action_sendto, Smstouri);  Intent.putextra ("Sms_body", "The SMS Text");  Send mms Uri Mmsuri = uri.parse ("Content://media/external/images/media/23");  Intent Intent = new Intent (intent.action_send);  Intent.putextra ("Sms_body", "The SMS Text");  Intent.putextra (Intent.extra_stream, Mmsuri);    Intent.settype ("Image/png");  Uninstall app Uri Uninstalluri = uri.fromparts ("package", "com.app.test", null);  Intent Intent = new Intent (Intent.action_delete, Uninstalluri); Install the application inTent Intent = new Intent (Intent.action_view);    Intent.setdataandtype (Uri.fromfile ("/sdcard/test.apk"), "application/vnd.android.package-archive");           Find app Uri uri in android Market = Uri.parse ("market://search?q= Angry Bird"); Intent Intent = new Intent (Intent.action_view, URI);




Note: Some need to configure certain permissions

Intent components in Android

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.