Android note 6. deep understanding of Intent and IntentFilter (2)

Source: Internet
Author: User

Android note 6. deep understanding of Intent and IntentFilter (2)
Deep understanding of Intent and IntentFiler (2) Jiangdg_VIP Http://blog.csdn.net/u012637501In the previous article, we learned more about Intent. Now we will learn how to set these attributes of Intent objects and how to use them to start components. An Intent object is a set of information. We can set its Action, Data, and Category attributes to specify which component to start and what Action to complete (including the Data required for the Action ). The intent is divided into intent and implicit intent. The so-called intent display refers to the Component that Intent has defined to start-implemented by specifying the Component Attribute of the Intent object; while the implicit intent, intent cannot determine which Component it will start (the Component attribute is not specified)-use AndroidManifest. the Intent Filter in the xml file is used to Filter components to determine the started components.1. explicit intentExplicit Intent, as the name implies, means that Intent has clearly defined the component that we can start. Because a Component class can be uniquely identified by its package name and class name, it can be implemented through the Component Attribute of intent. The Component Attribute of Intent needs to accept a ComponentName object. Its constructor passes in the package name and Class Name of the Component.1. Basic Idea of explicit intent Development(1) create a ComponentName object to specify the component package name and class name for the intent

ComponentName comp=new ComponentName(ComponentAttr.this,SecondaryActivity.class);
(2) create an Intent object and set the Component Attribute for this object.
Intent intent=new Intent();intent.setComponent(comp);
Note: by using different Intent constructors, steps (1) and (2) can be merged into: Intent intent = new Intent (ComponentAttr. this, SecondaryActivity. class); (3) Start an Activity component named SecondaryActivity.
StartActivity (intent); or startActivityForResult (intent, requestCode); // closes the started Activity and returns the result.
(4) Implement the SecondaryActivity class (inherited from Activity) of the started component (5) AddElement, but no need to configure Element
  
  
  
               
   
          
Blogger NOTE 1: In addition to the setComponent method of Intent, we can also use setClass to specify the specific component Intent intent = new Intent (); intent. setClass (ComponentAttr. this, SecondaryActivity. class); startActivity (intent );
Ii. Implicit intentImplicit intent, as its name implies, does not specify the component to be started by intent. Explicit intent can be implemented by setting its Component attribute, while implicit intent is implemented by Intent Filter. Specifically, we set the information (intent attribute) about the intent to start the component in advance, and then set the corresponding intent attribute in the AndroidManifest. xml file of other components. When a component sends an intent, the Android System Searches for other components in the project file AndroidManifest. xml (or system-level components ). And filter the components that meet the intent conditions. 1. Basic idea of using Action attributes for developmentIntent is the carrier for communication between components. component communication can be divided into communication between internal components of an application and communication between applications. The Action and Category attributes of Intent are common strings. Action represents an abstract Action requested by Intent. The Category attribute is used to add additional Category information to the Action. (1) Application internal component communication-custom string Mode
Public final String CUSTOME_ACTION = intent. action. CUSTOME_JIANG; // string can be any Intent intent = new Intent (); // create an Intent object intent. setAction (ActionAttr. CUSTOME_ACTION); // Note: ActionAttr is the startActivity (intent) class we have created. // start an Activity
(2) communicate with other applications-use the system's predefined action constant
Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); // Where ACTION_CALL is a static member variable of the Intent class, startActivity (intent) can be directly called by the class );
2. Basic development ideas using Action and Category attributes(1) Application internal component communication-custom string Mode
Public final String CUSTOME_ACTION = intent. action. CUSTOME_JIANG; // The String can be any public final String CUSTOME_CATEGORY = intent. action. CUSTOME_CATEGORY; // string can be any Intent intent = new Intent (); // create an Intent object intent. setAction (ActionAttr. CUSTOME_ACTION); // Note: ActionAttr is the intent class we have created. addCategory (ActionAttr. CUSTOME_CATEGORY); startActivity (intent); // start an Activity
(2) Use the system's predefined action and category constants-the following code to return the HOME desktop through the Intent object when a button is clicked.
Intent intent = new Intent (); intent. setAction (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); // return Home desktop startActivity (intent); // start an Activity. Note: AndroidManifest is not required. xml
For communication between internal components of the application, in addition to the preceding steps (1) (2), we also need to complete steps: (3) Implement the Activity to be started, such as SecondaryActivity. java, ThirdActivity. java, so that it inherits from Activity; (4) in the AndroidManifest project. add xmlAnd add Related information
 
Blogger NOTE 2: in fact, the Action and Category attributes are used together in the development of applications with intentions. Because the Android system will give the active Activity a default Action attribute and Category attribute in AndroidManifest. xml, that is: // Application entry In addition, pay attention to the following points: 1. attributes such as Action constants. For example, ACTION_CALL is used when intent is set, and its corresponding string is android. intent. action. CALL is in AndroidManifest. used in xml; 2. when you use a system predefined constant such as the Action attribute to communicate with other applications, you only need to use AndroidManifest in this application. add the corresponding permissions in xml. 3. Only one Action attribute and one Category attribute can be defined in an Activity. The default constant CATEGORY_DEFAULT is assigned to the Category attribute. 3. Source CodeThis instance provides two functions: (1) implement a button and start an Activity using the Action attribute and Category attribute; (2) implement a button to return to the HOME interface (1 ). firstActivity. java: Main Activity
Package com. example. android_intent_2; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class ActionCateAttr extends Activity {// customize an action constant org. crazyit. public final static String CRAZYIT_ACTION = intent. action. JIANG_ACTION; public final static String CRAZYIT_CATEGORY = intent. action. JIANG_CATEGORY; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. first); Button btn = (Button) findViewById (R. id. button); btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// create an Intent object Intent intent = new Intent (); intent. setAction (ActionCateAttr. CRAZYIT_ACTION); // sets the action attribute intent. addCategory (ActionCateAttr. CRAZYIT_CATEGORY); // sets the category attribute startActivity (intent) ;}}); // registers an event listener object for btn/* returns the desktop Button */Button btn1 = (Button) findViewById (R. id. home); btn1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// create an Intent object Intent intent = new Intent (); intent. setAction (Intent. ACTION_MAIN); // sets the action attribute intent. addCategory (Intent. CATEGORY_HOME); // sets the category attribute startActivity (intent) ;}}); // registers an event listener object for btn }}
(2). AndroidManifest. xml
    
        
        
                      
                       
                    
        
       
                  
     
    
(3) In the project, add SecondaryActivity. java and ThirdActivity. java to inherit from Activity.
(4) Effect
3. Basic ideas for developing Data and Type attributesThe Action attribute describes an Action for the Intent object. Therefore, the Data attribute provides operation Data for the Action attribute of the Intent object. The Type attribute is used to specify the MIME Type corresponding to the specified Uri of the Data. This Type can be any custom MIME Type, as long as the string conforms to the abc/xyz format. Note that the Type and Data attributes usually overwrite each other. If you want the Intent to have both the Data attribute and the Type attribute, you must use the setDataAndType () method. Note that the Data attribute only accepts one Uri object. A Uri object is usually represented by a string in the following format: Uri string format: scheme: // host: example of port/path: content: // com. android. contacts/1 or tel: // 18819463209 there are two situations: one is to start the system-level application; the other is to start the internal components of the application. The former does not need to be configured in AndroidManifest. xml You only need to add the corresponding permissions. In the latter, You need to configure Element Content. The Data and Type attributes of the component are declared as follows: Element in the following format: // Path string template of the Data Attribute Scenario 1: Start the system-level application component(1) Implement an Intent object and start the component
Intent intent = new Intent (); // create an Intent object String data = content: // com. android. contacts/1; Uri uri = Uri. parse (data); // converts a string to Uriintent. setAction (Intent. ACTION_VIEW); // sets the Intent object Action attribute intent. setData (uri); // set the Data attribute of the Intent object startActivity (intent); or Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); intent. setData (Uri. parse (content: // com. android. contacts/1); startActivity (intent );
(2) Add corresponding permissions to AndroidManifest. xml
When reading phone information in Android, pay attention to adding
        
         
When BroadcastReceiver is used in android
         
          
The
          
             
           
          
         
        
(3) Source Code the application implements three buttons to implement three functions: open a webpage, edit a contact, and call firstActivity. java.
Package com. android. android_intent_4; import android. support. v7.app. actionBarActivity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. content. intent; import android.net. uri; import android. OS. bundle; public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/* The first Button function: Open the webpage */Button btn1 = (Button) findViewById (R. id. button1); btn1.setOnClickListener (new OnClickListener () {// register an event listener object for button 1 @ Override public void onClick (View v) {// 1. create Intent intent = new Intent (); // 2. set action, data properties String data = http://www.baidu.com; Uri uri = Uri. parse (data); // convert the string to Uri-generic Resource Identifier intent. setAction (Intent. ACTION_VIEW); // set the intent attribute to the predefined Intent. ACTION_VIEW intent. setData (uri); // set data attributes for intent to pass data // 3. start Activity startActivity (intent) ;}});/* second key function: edit the contact marked as 1 */Button btn2 = (Button) findViewById (R. id. button2); btn2.setOnClickListener (new OnClickListener () {// register an event listener object for button 1 @ Override public void onClick (View v) {// 1. create Intent intent = new Intent (); // 2. set the action and data attributes intent. setAction (Intent. ACTION_EDIT); // set the intent attribute to the predefined Intent. ACTION_VIEW intent. setData (Uri. parse (content: // com. android. contacts/1); // sets the data attribute for intent and parses the Uri object based on the specified characters. // 3. start Activity startActivity (intent) ;}});/* Third key function: Call 18819465188 */Button btn3 = (Button) findViewById (R. id. button3); btn3.setOnClickListener (new OnClickListener () {// register an event listener object for button 1 @ Override public void onClick (View v) {// 1. create Intent intent = new Intent (); // 2. set the action and data attributes intent. setAction (Intent. ACTION_DIAL); // set the intent attribute to the predefined Intent. ACTION_VIEW intent. setData (Uri. parse (tel: 18819465188); // parse the Uri object based on the specified characters // 3. start Activity startActivity (intent );}});}}
The effect is as follows:
Scenario 2: Start internal application components(1) Implement an Intent object and start the component
Intent intent = new Intent (); // create an Intent object String data = lee: // www.fkjava.org: 8888/mypath; Uri uri Uri = Uri. parse (data); // converts a string to Uriintent. setAction (Intent. ACTION_VIEW); intent. setData (uri); // set the Data attribute of the Intent object startActivity (intent); or Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); intent. setData (Uri. parse (lee: // www.fkjava.org: 8888/mypath); startActivity (intent );
(2) set in AndroidManifest. xml Related content in the element.
  
(3) implement other activities

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.