Android tour-Intent and Intent Filter [on]

Source: Internet
Author: User

Intent indicates the Android app's "intention" to start. The Android app starts a specified component based on the Intent. The actual component to start depends on the Intent attributes.

1. explicit Intent

Specifies the Intent of the component to be started. We call it an explicit Intent.

For example:

Java code
  1. Package com. example. testintent;
  2. Import android. app. Activity;
  3. Import android. content. ComponentName;
  4. Import android. content. Intent;
  5. Import android. OS. Bundle;
  6. Import android. view. View;
  7. Import android. view. View. OnClickListener;
  8. Import android. widget. Button;
  9. Public class MainActivity extends Activity {
  10. Button button;
  11. @ Override
  12. Protected void onCreate (Bundle savedInstanceState ){
  13. Super. onCreate (savedInstanceState );
  14. SetContentView (R. layout. activity_main );
  15. Button = (Button) findViewById (R. id. button1 );
  16. Button. setOnClickListener (new OnClickListener (){
  17. @ Override
  18. Public void onClick (View arg0 ){
  19. ComponentName comp = new ComponentName (MainActivity. this, SecondActivity. class );
  20. Intent intent = new Intent ();
  21. Intent. setComponent (comp );
  22. StartActivity (intent );
  23. }
  24. });
  25. }
  26. }

 

Register SecondActivity in the manifest file.

 

The Componet attribute of Intent needs to receive a ComponentName object. The ComponetName object is actually a class that specifies the package and the Activity path to be started. It has the following structures:

Java code
  1. ComponentName (String pkg, String cls)
  2. ComponentName (Context pkg, String cls)
  3. ComponentName (Context pkg, Class <?> Cls)

 

ComponentName (Parcel in)

In addition to this property, Intent also contains the following three methods:

Java code
  1. SetClass (Context packageContext, Class <?> Cls)
  2. SetClassName (Context packageContext, String className)
  3. SetClassName (String packageName, String className)

 

The above code can be simplified to the following code:

Java code
  1. Intent intent = new Intent (MainActivity. this, SecondActivity. class );
  2. StartActivity (intent );

 

Ii. Implicit Intent

The Intent of the component to be started is not explicitly specified. We call it an implicit Intent.

In addition to the Componet attribute above, Intent also has the Action and Category attributes.

Action indicates an abstract Action to be completed by Intent, while Category indicates the Category information appended to the Action.

For example:

Java code
  1. Package com. example. testintent;
  2. Import android. app. Activity;
  3. Import android. content. ComponentName;
  4. Import android. content. Intent;
  5. Import android. OS. Bundle;
  6. Import android. view. View;
  7. Import android. view. View. OnClickListener;
  8. Import android. widget. Button;
  9. Public class MainActivity extends Activity {
  10. Button button;
  11. @ Override
  12. Protected void onCreate (Bundle savedInstanceState ){
  13. Super. onCreate (savedInstanceState );
  14. SetContentView (R. layout. activity_main );
  15. Button = (Button) findViewById (R. id. button1 );
  16. Button. setOnClickListener (new OnClickListener (){
  17. @ Override
  18. Public void onClick (View arg0 ){
  19. Intent intent = new Intent ();
  20. Intent. setAction ("com. example. intent. action. TEST_ACTION ");
  21. StartActivity (intent );
  22. }
  23. });
  24. }
  25. }

Configuration in the manifest File

Java code
  1. <Activity
  2. Android: name = ". SecondActivity">
  3. <Intent-filter>
  4. <Action android: name = "com. example. intent. action. TEST_ACTION"/>
  5. <Category android: name = "android. intent. category. DEFAULT"/>
  6. </Intent-filter>
  7. </Activity>

 

One Intent can only specify one Action attribute and can contain multiple Category attributes. When a program is created, the DEFAULT category component is started by DEFAULT.

Next, let's take a look at the usage of the Category attribute.

Java code
  1. Public void onClick (View arg0 ){
  2. Intent intent = new Intent ();
  3. Intent. setAction ("com. example. intent. action. TEST_ACTION ");
  4. Intent. addCategory ("android. intent. category. TEST_CATEGERY ");
  5. StartActivity (intent );
  6. }

Configuration in the manifest File

Java code
  1. <Activity
  2. Android: name = ". SecondActivity">
  3. <Intent-filter>
  4. <Action android: name = "com. example. intent. action. TEST_ACTION"/>
  5. <Category android: name = "android. intent. category. DEFAULT"/>
  6. <Category android: name = "android. intent. category. TEST_CATEGERY"/>
  7. </Intent-filter>
  8. </Activity>

It can be seen that the Activity to be started is determined based on the two attributes of Action and Category. There can be multiple Category, as long as one of them is satisfied.

 

In fact, Intent can not only start the Activity we define, but also start the Activity of the system and other applications.

  • ACTION_MAIN application portal
  • ACTION_VIEW: displays specified data
  • ACTION_ATTACH_DATA specifies that a piece of data will be appended to other places
  • ACTION_EDIT: Edit specified data
  • ACTION_PICK selects an item from the list and returns the selected data
  • ACTION_CHOOSER: displays an Activity selector.
  • ACTION_GET_CONTENT allows the user to select data and return the selected data
  • ACTION_DIAL display dial-up panel
  • ACTION_CALL calls a specified user directly.
  • ACTION_SEND send data to others
  • ACTION_SENDTO send messages to others
  • ACTION_ANSWER
  • Insert ACTION_INSERT
  • ACTION_DELETE delete data
  • ACTION_RUN run data
  • ACTION_SYNC: Execute Data Synchronization
  • ACTION_PICK_ACTIVITY is used to select Activity
  • ACTION_SEARCH
  • ACTION_WEB_SEARCH
  • ACTION_FACTORY_TEST factory test entry point

Only some parts are listed here

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.