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
- Package com. example. testintent;
- Import android. app. Activity;
- Import android. content. ComponentName;
- Import android. content. Intent;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Public class MainActivity extends Activity {
- Button button;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- Button = (Button) findViewById (R. id. button1 );
- Button. setOnClickListener (new OnClickListener (){
- @ Override
- Public void onClick (View arg0 ){
- ComponentName comp = new ComponentName (MainActivity. this, SecondActivity. class );
- Intent intent = new Intent ();
- Intent. setComponent (comp );
- StartActivity (intent );
- }
- });
- }
- }
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
- ComponentName (String pkg, String cls)
- ComponentName (Context pkg, String cls)
- ComponentName (Context pkg, Class <?> Cls)
ComponentName (Parcel in)
In addition to this property, Intent also contains the following three methods:
Java code
- SetClass (Context packageContext, Class <?> Cls)
- SetClassName (Context packageContext, String className)
- SetClassName (String packageName, String className)
The above code can be simplified to the following code:
Java code
- Intent intent = new Intent (MainActivity. this, SecondActivity. class );
- 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
- Package com. example. testintent;
- Import android. app. Activity;
- Import android. content. ComponentName;
- Import android. content. Intent;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Public class MainActivity extends Activity {
- Button button;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- Button = (Button) findViewById (R. id. button1 );
- Button. setOnClickListener (new OnClickListener (){
- @ Override
- Public void onClick (View arg0 ){
- Intent intent = new Intent ();
- Intent. setAction ("com. example. intent. action. TEST_ACTION ");
- StartActivity (intent );
- }
- });
- }
- }
Configuration in the manifest File
Java code
- <Activity
- Android: name = ". SecondActivity">
- <Intent-filter>
- <Action android: name = "com. example. intent. action. TEST_ACTION"/>
- <Category android: name = "android. intent. category. DEFAULT"/>
- </Intent-filter>
- </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
- Public void onClick (View arg0 ){
- Intent intent = new Intent ();
- Intent. setAction ("com. example. intent. action. TEST_ACTION ");
- Intent. addCategory ("android. intent. category. TEST_CATEGERY ");
- StartActivity (intent );
- }
Configuration in the manifest File
Java code
- <Activity
- Android: name = ". SecondActivity">
- <Intent-filter>
- <Action android: name = "com. example. intent. action. TEST_ACTION"/>
- <Category android: name = "android. intent. category. DEFAULT"/>
- <Category android: name = "android. intent. category. TEST_CATEGERY"/>
- </Intent-filter>
- </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