Intents are important for handling communication between Android components. We use it a lot in development, but many people don’t understand it very well. Today I want to summarize a few things about intents.
1. The Intent works in three parts
The Intent indicates where the communication request is coming from, where it is going, and how it is going. The initiator carries the data required for the communication, and the receiver unpacks the received Intent data. If the originator asks to determine the result of the recipient’s processing, the Intent is responsible for getting the recipient to return the data content of the reply.
2. Parameters related to the Intent
The element name | Set the method | Description and Usage |
---|---|---|
Component | setComponent | Component that specifies the source and destination of the Intent |
Action | setAction | Action, which specifies the action behavior of the Intent |
Data | setData | The Uri specifies the data path that the action will manipulate |
Category | addCategory | Category, used to specify the action category of the Intent |
Type | setType | Data type, used to specify the data type of the message |
Extras | putExtra | Extended information that specifies parameter information for loading |
Flags | setFlags | Flag bit that specifies the mode in which the Intent is to run (start flag) |
3. Three types of explicit Intent invocation
(1) Specify in the constructor. Example code is as follows:
Intent intent = new Intent(this,ActResponseActivity.class); // Create an intent with a goal definedCopy the code
(2) Call setClass method to specify, example code is as follows:
Intent intent = new Intent(); / / create a new intent intent. SetClass (this, ActResponseActivity. Class); // Sets the activity class to jump toCopy the code
(3) Call the setComponent method to specify the following code:
Intent intent = new Intent(); / / create an intention the ComponentName component = new the ComponentName (this, ActResponseActivity. Class); intent.setComponent(component); // Sets the component information intended to carryCopy the code
4. System action with an implicit Intent
The name of the system action constant of the Intent class | Constant value of system action | instructions |
---|---|---|
ACTION_MAIN | android.intent.action.MAIN | Entry for App startup |
ACTION_VIEW | android.intent.action.VIEW | Display data to the user |
ACTION_EDIT | android.intent.action.EDIT | Displays editable data |
ACTION_SEND | android.intent.action.SEND | Share content |
ACTION_CALL | android.intent.action.CALL | Direct dialing |
ACTION_DIAL | android.intent.action.DIAL | Ready to dial |
ACTION_SENDTO | android.intent.action.SENDTO | Send a text message |
ACTION_ANSWER | android.intent.action.ANSWER | Answer the phone |
ACTION_SEARCH | android.intent.action.SEARCH | Search action for SearchView on the navigation bar |
5. Example for Making a Call (Direct Call)
Intent intent = new Intent(); // Create a new intent intent.setAction(intent.action_call); Parse ("tel:"+phone); // Set the intent action to direct dial Uri Uri = uri.parse ("tel:"+phone); // Declare a dial Uri intent.setData(Uri); // Set the intended path startActivity(intent); // Launch the active page intended to lead toCopy the code
6. Entry action configuration
<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <actegory android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>Copy the code
7. Send an SMS message with an implicit Intent
Intent intent = new Intent();
intent.setAction("android.intent.action.SENDTO");
intent.setData(Uri.parse("smsto:"+"15536935920"));
intent.putExtra("sms_body","小熊熊写代码发送的短信");
startActivity(intent);
Copy the code
8. Example for Making a Call (To be dialed)
Intent intent = new Intent(); // Create a new intent intent.setAction(intent.action_dial); Parse ("tel:"+"15835133570"); // Set intent action to direct dial Uri Uri = uri.parse ("tel:"+"15835133570"); // Declare a dial Uri intent.setData(Uri); // Set the intended path startActivity(intent); // Launch the active page intended to lead toCopy the code
9. Pass the parameters to the next page
Intent intent = new Intent(MainActivity.this,FirstActivity.class); Bundle = new Bundle(); // Create a bundle. Bundle.putstring ("name"," zhang SAN "); PutInt ("age",30); PutDouble ("height",170.0f); putDouble("height",170.0f); // Store a double intent.putextras (bundle); StartActivity (intent); // Launch the activity page desired by the intentCopy the code
10. Accept the parameters passed from the previous page
Intent intent = getIntent(); Intent.getextras (); intent.getextras (); intent.getextras (); String name = bundle.getString("name",""); Int age = bundle.getInt("age",0); int age = bundle.getInt("age",0); Double height = bundle.getDouble("height",0.0f); // Retrieve the double from the packageCopy the code
11. Return parameters to the previous page
Intent intent = new Intent(); // Create a new intent Bundle = new Bundle(); // Create a bundle. PutString ("job"," code "); // Store a string intent.putextras (bundle); SetResult (activity.result_ok,intent); // Return to the previous page with intent finish(); // Close the current pageCopy the code
12. The previous page receives parameters
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle resp = data.getExtras();
String job = resp.getString("job");
}
Copy the code