Describe how to use Android Intent

Source: Internet
Author: User

The premise for Android Intent matching is to match the Intent values: Action, Category, Data/Type. If Component is filled with Componet, It is the Test in the previous example. class.

1. If you enter Action, if there is a program Manifest. if the IntentFilter segment of an Activity in xml defines the same Action, the Intent matches the target Action. If Type and Category are not defined in the Filter segment, the Activity matches. However, if there are more than two matching programs on the phone, a dialog box will pop up to indicate the matching.

  • In-depth description of Android program conversion
  • Details about Android plug-ins
  • Complete parsing of Android applications
  • Get started with Android applications
  • In-depth analysis of four construction blocks of Android applications

The value of Action has many predefined meanings in Android. If you want to directly go to your own Intent receiver, you can add a custom Action value to the receiver's Android Intentr and set the Category value to "android. intent. category. DEFAULT "). Set this value to the Intent Action in your Android Intent to directly jump to your own Intent receiver. This Action is unique in the system.

2. data/type. You can use Uri as data, for example, Uri uri = Uri. parse (http://www.google.com); Intent I = new Intent (Intent. ACTION_VIEW, uri); During the Intent delivery process of the mobile phone, the data type is determined based on the scheme of the http://www.google.com. The phone's Brower can match it. In Brower's Manifest. xml, IntenFilter first contains ACTION_VIEW Action, which can also process http: type.

3. For classification Category, do not set it in Intent. If you write the Intent receiver, it is in Manifest. the IntentFilter of xml Activity contains android. category. DEFAULT. In this way, CategoryIntent is not set. the Intent of addCategory (String c);) will match this Category.

4, extras additional information), is a set of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.

Sample Code:

 
 
  1. public class HelloActivity extends Activity {   
  2.    
  3.  @Override  
  4.  public boolean onCreateOptionsMenu(Menu menu) {  
  5.   // TODO Auto-generated method stub  
  6.   super.onCreateOptionsMenu(menu);  
  7.   menu.add(0, Menu.FIRST+1, 1, R.string.menu_open);  
  8.   menu.add(0, Menu.FIRST+2, 2, R.string.menu_edit);  
  9.   menu.add(0, Menu.FIRST+3, 3, R.string.menu_update);  
  10.   menu.add(0, Menu.FIRST+4, 4, R.string.menu_close);  
  11.   return true;  
  12.  }  
  13.  
  14.  @Override  
  15.  public boolean onOptionsItemSelected(MenuItem item) {  
  16.   // TODO Auto-generated method stub  
  17.   super.onOptionsItemSelected(item);  
  18.   switch(item.getItemId())  
  19.   {  
  20.   case Menu.FIRST + 1:  
  21.   {  
  22.    this.setTitle("Open Text!");  
  23.    Intent i = new Intent();     
  24.    i.setAction("test_action");    
  25.    if (Tools.isIntentAvailable(this,i))  
  26.     this.startActivity(i);  
  27.    else  
  28.     this.setTitle("the Intent is unavailable!!!");  
  29.    break;  
  30.   }  
  31.   case Menu.FIRST + 2:  
  32.   {  
  33.    this.setTitle("Edit Text!");  
  34.    break;  
  35.   }  
  36.   case Menu.FIRST + 3:  
  37.   {  
  38.    this.setTitle("Update Text!");  
  39.    break;  
  40.   }  
  41.   case Menu.FIRST + 4:  
  42.   {  
  43.    this.setTitle("Close Text!");  
  44.    break;  
  45.   }  
  46.   }  
  47.   return true;  
  48.  } 

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.