Intent objects in Android are fully parsed _android

Source: Internet
Author: User
Tags stub home screen

I. The role of Intent
Intent is an abstract description of the action to be performed, typically used as a parameter, by Intent to assist in the communication between the various components of the Android. For example, call StartActivity () to start an activity, or by broadcaseintent () to all interested broadcasereceiver, or by StartService ()/ Bindservice () to start a backend service. So it can be seen that intent is primarily used to initiate other activity or service, so that intent can be understood as a binder between activity.

Ii. composition of the intent
to pass data between different activity, it is necessary to include something in the intent, and generally the most basic data should include:
Action is used to indicate what action to implement, such as Action_view, Action_edit, and so on. You can refer to the Android.content.intent class in the Android sdk-> reference, where all the action is defined in constants.
Data to the facts of the concrete, generally by a URI variable to represent

Here are some simple examples:

Action_view CONTENT://CONTACTS/1//displays information for a contact with a identifier of 1.
action_dial CONTENT://CONTACTS/1//Call this contact.

In addition to the two most basic elements of action and data, intent includes some other elements,
Category (category): This option specifies additional information about the action that will be executed, such as launcher_category that the intent recipient should appear as a top-level application in LAUNCHER, and Alternative_ Category indicates that the current intent is one of a series of optional actions that can be performed on the same piece of data. You can also refer to the Android.content.intent class in the Android sdk-> reference. I have also written a article on category, click here to view.
Type (data type): explicitly specifies the intent data type (MIME). The data type of a general intent can be judged by the data itself, but by setting this property, you can force an explicitly specified type to no longer be inferred.
Component (component): Specifies the class name of the target component of the intent. Typically, Android looks for information about other attributes contained in intent, such as action, Data/type, category, and eventually finds a target component to match. However, if component this attribute is specified, the component specified by it is used directly, and the lookup process is no longer performed. When this property is specified, all other properties of intent are optional.
Extras (additional information), is a collection of all other additional information. With extras, you can provide extended information for your component, for example, if you want to perform the "Send e-mail" action, you can save the header, body, etc. of the e-mail message in extras and send the component to the e-mail message.
Here are a few examples of these additional attributes:
Action_main with category Category_home//Used to Launch home screen. I have also written an article about it before, click here to see.
Action_get_content with MIME type vnd.android.cursor.item/phone//Phone number for everyone in the list
As you can see, action, Data/type, category, and extras form a language that can be used by Android to express phrases such as "Call John".

Three, intent analysis
the component of an application can declare one or more intent Filter to tell Android that it can respond to and process which implicit intent requests. Each intent filter describes the ability of the component to respond to intent requests-what type of request behavior the component wants to receive, and what type of request data. For example, before requesting a Web browser, the Web browser program's intent filter should declare that the intent ACTION it wants to receive is web_search_action, and that the requested data associated with it is the Web page address URI format. How do I declare my intent Filter for a component? A common method is to describe the Intent Filter of a component using attributes < intent-filter> in the Androidmanifest.xml file.
As we mentioned earlier, the three elements of the comparison between implicit intent (Explicit Intents) and Intent Filter (implicit Intents) are intent actions, data, and categories. In fact, an implicit intent request can be passed to the target component, and it is necessary to pass these three aspects of the check. If any of these mismatches do not match, Android will not pass the implicit intent to the target component. Next we explain the specific rules of these three aspects of inspection.
1. Action test

< intent-filter> elements can include child elements < Action>, such as:

< intent-filter> 

< action android:name= "Com.example.project.SHOW_CURRENT"/> 

< action Android : Name= "Com.example.project.SHOW_RECENT"/>

 < action android:name= "Com.example.project.SHOW_PENDING"/ > 

</intent-filter> 

A < intent-filter> element should contain at least one < Action>, or no intent request can match the < intent-filter>. If the action of the intent request matches one of the < action> in the < intent-filter>, then the intent passes this < intent-filter> action test. If no specific action type is specified in the intent request or < intent-filter>, the following two scenarios occur.
(1) If the < intent-filter> does not contain any action type, then no intent request can match this < intent-filter>;
(2) Conversely, if the action type is not set in the intent request, the intent request will smoothly pass < intent-filter> if the action type is included in the < intent-filter> Behavior Test.
2. Category test

< intent-filter> elements can contain < category> child elements, such as:

< Intent-filter >

 < category android:name= "Android. Intent.Category.DEFAULT "/>

 < Category android:name=" Android. Intent.Category.BROWSABLE "/> 

</intent-filter> 

The intent request passes the test only if all category in the intent request exactly matches the < category> of a intentfilter in the component, intentfilter the redundant < category The > declaration does not cause a match to fail. A intentfilter that does not specify any category test will only match intent requests that do not have a set category.
3. Data test
the data is described in < intent-filter> as follows:

< Intent-filter >

 < Data android:type= "Video/mpeg" android:scheme= "http" .../>

 < data Android:type= "audio/mpeg" android:scheme= "http" .../>

 </intent-filter> 

The element specifies the data URI and data type of the intent request that you want to accept, and the URI is divided into three parts to match: scheme, authority, and path. The URI data type and scheme of the Inteat request set with SetData () must be consistent with the specified in Intentfilter. If a authority or path is also specified in the Intentfilter, they also need to match to pass the test.
4. A simple example illustrates
after explaining the basics of intent, let's use intent to activate the phone dialer with Android, and by this example you'll find that using intent is not as difficult as its concept describes. The code that eventually creates the intent is shown below.

Intent i = new
Intent (Intent.action_dial,uri.parse ("Tel://13800138000″)");

Once you've created the intent, you can tell Android that you want to start a new activity.

StartActivity (i);

The activity starts with the following display interface:


Three, intent constructors
Public Constructors:
1, Intent () empty constructor
2, Intent (Intent o) copy constructor
3, Intent (String action) to specify the constructor of the action type
4, Intent (String action, URI URI) specifies the action type and the URI's constructor, which is primarily a combination of data sharing between programs ContentProvider
5, Intent (context Packagecontext, class<?> CLS) The constructor for the incoming component, which is referred to above
6, Intent (String action, Uri Uri, context Packagecontext, class<?> CLS) The first two kinds of binding body
Intent has six constructors, 3, 4, and 5 are the most common, not useless!
The action of the Intent (String action, Uri URI) is the value of the Name property corresponding to the action node in Androidmainfest.xml. Many action and category constants are defined in the intent class.
Example code two:

Intent Intent = new Intent (intent.action_edit, null);
StartActivity (Intent);

Example code two uses the fourth constructor, except that the URI parameter is null. When this code is executed, the system will look for <action android:name= "Android.intent.action.EDIT"/> Corresponding activity in the program master configuration file Androidmainfest.xml , if it corresponds to multiple activity with <action android:name= "Android.intent.action.EDIT"/> A dailog selection activity will pop up at this point.
This is not the case if you send it in the same way as sample code.
Using intent to pass data between activity
Execute the following code in main:

 Bundle Bundle = new Bundle ();
 Bundle.putstringarray ("Namearr", Namearr);
 Intent Intent = new Intent (main.this, countlist.class);
 Intent.putextras (bundle);
 StartActivity (Intent);

In Countlist, the code is as follows:

 Bundle Bundle = This.getintent (). Getextras ();
 string[] Arrname = Bundle.getstringarray ("Namearr");

The above code realizes the data transfer between the activity!


Iv. Summary of common methods
This article is I just started to learn Android when I saw, at that time is not very in-depth understanding, and now look back to this article summarized in detail, here and share with you.
1, calling a Web browser

Uri Mybloguri = Uri.parse ("http://kuikui.javaeye.com");
Returnit = new Intent (Intent.action_view, Mybloguri);

2, Map

Uri Mapuri = Uri.parse ("geo:38.899533,-77.036476");
Returnit = new Intent (Intent.action_view, Mapuri);

3, transfer the telephone interface

Uri Teluri = Uri.parse ("tel:100861");
Returnit = new Intent (intent.action_dial, Teluri);

4, call directly

 
Uri Calluri = Uri.parse ("tel:100861");
Returnit = new Intent (Intent.action_call, Calluri);

5, uninstall

Uri Uninstalluri = uri.fromparts ("package", "XXX", null);
Returnit = new Intent (Intent.action_delete, Uninstalluri);

6, installation

Uri Installuri = uri.fromparts ("package", "XXX", null);
Returnit = new Intent (intent.action_package_added, Installuri);

7, play

Uri Playuri = Uri.parse ("File:///sdcard/download/everything.mp3");
Returnit = new Intent (Intent.action_view, Playuri);

8, call Send mail

Uri Emailuri = Uri.parse ("mailto:shenrenkui@gmail.com");
Returnit = new Intent (intent.action_sendto, Emailuri);

9, send an email

Returnit = new Intent (intent.action_send);
String[] tos = {"shenrenkui@gmail.com"};
String[] CCS = {"shenrenkui@gmail.com"};
Returnit.putextra (Intent.extra_email, TOS);
Returnit.putextra (INTENT.EXTRA_CC, CCS);
Returnit.putextra (Intent.extra_text, "body");
Returnit.putextra (Intent.extra_subject, "SUBJECT");
Returnit.settype ("message/rfc882");
Intent.createchooser (Returnit, "Choose Email Client");

10, send text messages

Uri Smsuri = Uri.parse ("tel:100861");
Returnit = new Intent (Intent.action_view, Smsuri);
Returnit.putextra ("Sms_body", "Shenrenkui");
Returnit.settype ("vnd.android-dir/mms-sms");

11, send mail directly

Uri Smstouri = Uri.parse ("smsto://100861");
Returnit = new Intent (intent.action_sendto, Smstouri);
Returnit.putextra ("Sms_body", "Shenrenkui");

12, send MMS

Uri Mmsuri = Uri.parse ("Content://media/external/images/media/23");
Returnit = new Intent (intent.action_send);
Returnit.putextra ("Sms_body", "Shenrenkui");
Returnit.putextra (Intent.extra_stream, Mmsuri);
Returnit.settype ("Image/png");

It's OK to call StartActivity (Returnit) directly with the intent you get.

V. Eg:

 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns : tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_" Parent "android:paddingbottom=" @dimen/activity_vertical_margin "android:paddingleft=" @dimen/activity_horizontal_ Margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_ Margin "tools:context=". Mainactivity "> <button android:id=" @+id/button1 "android:layout_height=" Wrap_content "Android:la" Yout_width= "Match_parent" android:text= "Start a second activity"/> </RelativeLayout> 
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" match_parent " 
  android:layout_height=" match_parent " 
  android:o" rientation= "vertical" > 
  <textview  
    android:id= "@+id/text1" android:layout_width= "Match_parent" 
    android:layout_height= "wrap_content" 
    /> 
 
</LinearLayout> 


Package com.android.xiong.intent_one; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
 
Import Android.widget.Button; 
  public class Mainactivity extends activity {private Button button1; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.activity_main); 
    button1= (Button) Findviewbyid (R.id.button1); 
    OnClick on=new onclick (); 
  Button1.setonclicklistener (on); Class OnClick implements onclicklistener{@Override public void OnClick (View arg0) {//TODO A 
      uto-generated method Stub Intent Intent =new Intent (); 
      Intent.setclass (Mainactivity.this, Twoactivity.class); 
      String [] name={"Zhangsan", "lis", "Wangwu"}; 
      int []age={12,13,14}; Intent.putextra ("Com.android.xiong.intent_one.") Name ", name); Intent.putextra ("Com.android.xiong.intent_one.") 
      Age ", age); 
       
       
    StartActivity (Intent); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the ' menu; this adds items T 
    o The Action Bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 
 } 
 
}


Package com.android.xiong.intent_one; 
 
Import Java.util.HashMap; 
 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
 
public class Twoactivity extends activity{ 
  private TextView Text1; 
 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { 
    //TODO auto-generated method stub 
    Super.oncreate (savedinstancestate); 
    Setcontentview (r.layout.activity_two); 
    text1= (TextView) Findviewbyid (R.ID.TEXT1); 
    Intent intent=getintent (); 
    String []name=intent.getstringarrayextra ("Com.android.xiong.intent_one. Name "); 
    int [] Age=intent.getintarrayextra ("Com.android.xiong.intent_one. Age "); 
    String print= ""; 
    for (int i = 0; i < age.length i++) { 
      print+= ' name: ' +name[i].tostring () + ' Age: ' +age[i]+ ' \ n '; 
    } 
    Text1.settext (print); 
   
 


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.