Summary of Android Intent application practices

Source: Internet
Author: User

Friends who often read our BKJIA technical articles may be impressed that operations related to the Android Intent application will appear in the previous Android local operations. Here we will introduce the Android Intent application in detail.

In an Android Application, the main components are Activity, Service, ContentProvider, etc.). Intent assists in communication between these components.

As some people on the Internet said, Intent describes the actions, actions involving data, and additional data in an application. Android is responsible for finding the corresponding components based on the description of this Intent, pass Intent to the called component and complete the call of the component. Intent serves as a decoupling between the caller and the called.

During the Intevnt transfer process, you need to find another Activity of the target consumer, IntentReceiver or Service), that is, the responder of Intent. There are two methods to match:

Android Intent app 1, show matching Explicit ):

 
 
  1. public TestB extents Activity  
  2. {  
  3. .........  
  4. };  
  5. public class Test extends Activity  
  6. {  
  7. ......  
  8. public void switchActivity()  
  9. {  
  10. Intent i = new Intent(Test.this, TestB.class);  
  11. this.startActivity(i);  
  12. }  

The code is concise and clear. After the switchActivity () function is executed, it will jump to the Activity named TestB immediately.

  • Basic Application Methods of Android single queue
  • Analysis of Android output Log related application skills
  • Android local broadcast Operation Analysis
  • Android Widget Toolkit
  • Basic concepts of the Android Menu System

Android Intent Application 2, Implicit match (Implicit ):

For implicit match, you must first match the values of Intent: Action, Category, Data/Type, and Component.
If you enter Componet, It is Test. class in the previous example.) This forms a display match. Therefore, this section only describes the first few matching types. The matching rule is the maximum matching rule,

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 the Filter segment does not define Type, Category, then 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.

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 IntentFilter and set the Category value to "android. intent. category. DEFAULT "), set this value to the Intent Action in your Intent, you can 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 ();

Intent I = new Intent (Intent. ACTION_VIEW, uri); During the Intent delivery process of the mobile phone, the data type is determined according to 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.

Android Intent application 3. Example code:

 
 
  1. public class HelloActivity extends Activity {   
  2. @Override  
  3. public boolean onCreateOptionsMenu(Menu menu) {  
  4. // TODO Auto-generated method stub  
  5. super.onCreateOptionsMenu(menu);  
  6. menu.add(0, Menu.FIRST+1, 1, R.string.menu_open);  
  7. menu.add(0, Menu.FIRST+2, 2, R.string.menu_edit);  
  8. menu.add(0, Menu.FIRST+3, 3, R.string.menu_update);  
  9. menu.add(0, Menu.FIRST+4, 4, R.string.menu_close);  
  10. return true;  

 
 
  1. @Override  
  2. public boolean onOptionsItemSelected(MenuItem item) {  
  3. // TODO Auto-generated method stub  
  4. super.onOptionsItemSelected(item);  
  5. switch(item.getItemId())  
  6. {  
  7. case Menu.FIRST + 1:  
  8. {  
  9. this.setTitle("Open Text!");  
  10. Intent i = new Intent();   
  11. i.setAction("test_action");   
  12. if (Tools.isIntentAvailable(this,i))  
  13. this.startActivity(i);  
  14. else  
  15. this.setTitle("the Intent is unavailable!!!");  
  16. break;  
  17. }  
  18. case Menu.FIRST + 2:  
  19. {  
  20. this.setTitle("Edit Text!");  
  21. break;  
  22. }  
  23. case Menu.FIRST + 3:  
  24. {  
  25. this.setTitle("Update Text!");  
  26. break;  
  27. }  
  28. case Menu.FIRST + 4:  
  29. {  
  30. this.setTitle("Close Text!");  
  31. break;  
  32. }  
  33. }  
  34. return true;  

 
 
  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3. super.onCreate(savedInstanceState);   
  4. this.setContentView(R.layout.main);   
  5. }  
  6. }  
  7. public class TestIntent extends Activity {  
  8. @Override  
  9. protected void onCreate(Bundle savedInstanceState) {  
  10. // TODO Auto-generated method stub  
  11. super.onCreate(savedInstanceState);  
  12. TextView tv = new TextView(this);  
  13. tv.setText("Testing Intent here!");  
  14. this.setContentView(tv);   
  15. }  

Let's take a look at the Manifest. xml of the project where TestIntent is located.

 
 
  1. ....  
  2. <activity android:name="TestIntent" android:label="@string/hello">
  3. <intent-filter> 
  4. <action android:name="test_action"></action> 
  5. <category android:name="android.intent.category.DEFAULT"> 
  6. </category> 
  7. </intent-filter> 
  8. </activity> 
  9. ..... 

The Android Intent application is described here.

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.