Android self-study note-14-Intent (Intent)

Source: Internet
Author: User

Intent is an abstract description of the action to be executed. It is generally used as a parameter and is used by Intent to facilitate communication between android components. Intent in Android can be understood as a media or messenger for communication between different components. Therefore, Intent is mainly used to call an Activity or Service.

Intentions are divided into explicit and implicit intentions. It is generally convenient for explicit use. If we call our own Activity, we can start it directly. If we call other activities, at this time, we need to get the package name and Class Name of the Activity we want to start, and then call it.

You can use the following method to call a self-written Activity:

Intent intent = new Intent(this, SecondActivity.class);startActivity(intent);
If you need to call others' activities, you need to determine the Class Name of the package name. However, sometimes we do not know the class name of the Activity package name. At this time, we need to click the Activity we want to call on the device or simulator. the browser is used as an example, in this case, the following output is printed in logcat:
02-24 23:11:19.652: I/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.android.browser/.BrowserActivity } from pid 137

At this time, we need to pay attention to cmp = com. android. browser /. browserActivity, through which we can determine the package name is com. android. browser, Class Name: com. android. browser. browserActivity. In this case, you can use the following code to call the Activity:

Intent intent = new Intent();intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");startActivity(intent);
All of the above are explicit, but they also have their disadvantages. If we call other people's Activity, and if another person's package name class name is changed, then we will fail to call it, to cope with this situation, the implicit Intent provided by Android requires many parameters for our Intent when calling the implicit Intent, then, the Android system calls the corresponding Activity based on these parameters (for more information about the implicit intent, please refer to Baidu ). If an Activity is called by implicit intent, the corresponding parameters must be configured in AndroidManifest. xml. Here is an example of Configuration:
            
                                 
                  
                  
                  
                  
                  
              
         
An Activity is configured and can be called by implicit intent. The following code is called:
Intent intent = new Intent (); intent. setAction ("com. mxy. intenttest ");/** add category: * android is automatically added when startActivity (intent) starts Activity. intent. category. DEFAULT category. Therefore, you need to * configure this category in Manifest. however, you do not need to explicitly add this Category in the program. * only when all the Category in the Intent request and one of the IntentFilter
 
  
When the match is complete, * will make the Intent request pass the test.
  
   
The statement does not cause a matching failure. * An IntentFilter that does not specify any category for testing only matches Intent requests that do not set a category. * /// Intent. addCategory ("android. intent. category. DEFAULT ");/** set data: In the API, this method will clear the type set for the setType Method */intent. setData (Uri. parse ("http://www.andyidea.com: 8080/person");/** set type: the API says this method will clear the data set by the method above setData */intent. setType ("vnd. androd. cursor. item/test ");/** set both data and type: if the Intent filter contains both data and data type settings *, you can only use this method to set the data and type */intent. setDataAndType (Uri. parse ("http://www.andyidea.com: 8080/person"), "vnd. androd. cursor. item/test "); startActivity (intent );
  
 
The above code calls a self-written Activity. In fact, the Android System also provides a lot of implicit intent for us to call. The following is an example:
Intent intent = new Intent();    intent.setAction(Intent.ACTION_VIEW);    intent.setData(Uri.parse("http://www.baidu.com"));    startActivity(intent);

After the intention is introduced, values are usually transferred during the switching of two activities. Intent is also required for transferring values. If we want to pass a value to the Activity to be started, we need to write it in the following format:
Intent intent = new Intent(this, SecondActivity.class);    intent.putExtra("name", "mengxiangyue");    startActivity(intent);
Then, use the following code in the started Activity to receive the passed value:
Intent intent = this.getIntent();String name = "";if(intent.getExtras() != null){name = intent.getExtras().getString("name");Toast.makeText(this, name, Toast.LENGTH_LONG).show();}

The value is passed. Sometimes we want to pass some values to the Activity that calls the started Activity when it is closed. At this time, we need to use the following code when calling:
Intent intent = new Intent (this, ThirdActivity. class); intent. putExtra ("name", "mengxiangyue"); startActivityForResult (intent, 1); // 1 indicates the Request Code
Execute the following code in the started Activity:
Intent intent = new Intent (this, SecondActivity. class); intent. putExtra ("back", "this is the returned value"); setResult (2, intent); finish ();
At this time, the data is passed back and needs to be received in the Activity. At this time, you need to implement the following methods:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {System.out.println("requestCode:" + requestCode + " resultCode:" + resultCode + "  data:" + data.getStringExtra("back"));Toast.makeText(this, data.getStringExtra("back"), Toast.LENGTH_LONG).show();super.onActivityResult(requestCode, resultCode, data);}
This method receives two parameters: requestCode and resultCode. These two parameters can uniquely determine the results of a request to prevent confusion in the returned results.

Example of Engineering: http://download.csdn.net/detail/mengxiangyue/7242667





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.