Use of implicit Intent-Android study Note 3
Use of implicit Intent
1. Why use implicit Intent?
However, if you want to call components of other programs, and developers often do not know the component names of other applications, you can only use the implicit Intent. The implicit Intent is the opposite, it does not define the target component to be activated with the component name, but Android helps the application find the component that best matches the Intent request Intent.
2. How to Find the Android system?
Intent Filter is used to find objects related to implicit Intent. The specific method is: Android sends the Intent request content Comparison: Intent Filter contains all possible components to be selected in the system. If If a component matches the content of the implicit Intent request, Android selects this component as the target component of the hidden Intent.
III, The basic format is as follows:
4. How does the Intent request content match Comparison?
1. Action Comparison-(step 1) For example:
1). One The element must contain at least one. Otherwise, no Intent request can match Match.
2). If the Intent request Action and The Intent will pass the match. . (Step 1)
2. Category comparison- (Step 2) for example:
1). Only when all the Category in the Intent request and the Intent-filter The matching is successful only when the match is complete. That is to say, The category in must contain all the category in the Intent, so that the category match is successful. (Step 2)
2). Unnecessary in Intent-filter The statement does not cause a matching failure, that is The category in can have additional items;
3). It is a default category. When startActivity () is called, the category is automatically added to the intent.
4 ). each implicit Intent sent by startActivity () method has at least one category, which is "android. intent. category. DEFAULT ", so any Activity that wants to receive an implicit Intent should include
"Android. intent. category. DEFAULT" category. Otherwise, Intent matching fails.
3. data comparison-(Step 3) for example:
1 ). The element specifies the data URI and Data Type of the Intent request to be accepted. The URI is divided into three parts for matching: scheme, authority, and path. The URI data type and scheme of the Intent request set with setData () must be consistent with that specified in Intent-filter. If authority or path is also specified in Intent-filter, they also need to match to pass the test. (Step 3)
Total:
The matching will continue only when the first step matches successfully. And the second step. When all of them match, find the corresponding components that contain them and jump to the corresponding target component.
5. Common usage of Intent. Action (Android built-in Action:
1. Intent. ACTION_MAIN
String: android. intent. action. MAIN
Purpose: Identify Activity as the beginning of a program (indicating the Activity that the app executes first ). It is commonly used.
Input: nothing
Output: nothing
Example:
For example, use Intent to return to the Home desktop of the system.
Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME);startActivity(intent);
2. Intent. Action_CALL
Stirng: android. intent. action. CALL
Purpose: Call the specified phone number (call directly ).
Input: phone number. Data format: tel: + phone number
Output: Nothing
Example:
Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); // sets the current action to call intent. setData (Uri. parse ("tel: 1320010001"); // set the call startActivity (intent );
In the AndroidMainifest. xml file, set the dialing permission for the current program:
3. Intent. Action. DIAL
String: action. intent. action. DIAL
Purpose: Call the dial-up panel (only call, and no call is started)
Input: phone number. Data format: tel: + phone number
Output: Nothing
Note: Open the dialing UI of Android. If no data is set, an empty UI is opened;
Example:
Intent intent = new Intent (); intent. setAction (Intent. ACTION_DIAL); // sets the current action to call the dial intent. setData (Uri. parse ("tel: 1584014 xxxx"); // you can specify the call startActivity (intent );
(An example is provided to illustrate how to call a phone, including direct dialing, opening a dial, and custom dialing)
// Button click event-call Button btn_call = (Button) findViewById (R. id. call); btn_call.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); // sets the current action to call intent. setData (Uri. parse ("tel: 1584014 xxxx"); // set the call startActivity (intent );}}); // Button click event-call the call Button btn_dial = (Button) findViewById (R. id. dial); btn_dial.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_DIAL); // sets the current action to call the dial intent. setData (Uri. parse ("tel: 1584014 xxxx"); // you can specify the call startActivity (intent );}}); // obtain the EditTextfinal EditText edt_phoneNumber = (EditText) findViewById (R. id. phonebunber_id); // Button click event-user dialing Button btn_userCall = (Button) findViewById (R. id. userCall); btn_userCall.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// verify the validity of the phone number entered by the user (use the package provided by Android to determine the validity of the phone number) String phoneNumber = edt_phoneNumber.getText (). toString (); if (PhoneNumberUtils. isGlobalPhoneNumber (phoneNumber) {Intent intentDial = new Intent (); intentDial. setAction (Intent. ACTION_CALL); // set intentDial for custom dialing currently. setData (Uri. parse ("tel:" + phoneNumber); startActivity (intentDial); // prompt that the user's phone number is incorrect} else {Toast. makeText (CallActivity. this, "the phone number you entered is incorrect. Please enter it again", Toast. LENGTH_SHORT ). show ();}}});
4. Intent. ACTION_VIEW
String: android. intent. action. VIEW
Purpose: This function is used to display user data. It is common and the Activity is opened based on the user data type.
Data type refers to: tel (Open dial Activity), http (open browser Activity), geo (open map location Activity), etc.
Note: several simple applications are demonstrated here;
// Button click event-Open Map Button btn_geo = (Button) findViewById (R. id. geo); btn_geo.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); // sets the intent of the current action. setData (Uri. parse ("geo: 39.899533, 116.036476"); // open map positioning (directly open the map software on your mobile phone) startActivity (intent );}}); // Button click event-open the browser Button btn_http = (Button) findViewById (R. id. http); btn_http.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); // sets the intent of the current action. setData (Uri. parse ("http://www.baidu.com"); // open the browser and open Baidu startActivity (intent) ;}}); // Button click event-open a photo Button btn_image = (Button) findViewById (R. id. image); btn_image.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); // set the current action // if you are not clear about your storage path, you can view Uri uri = Uri in DDMS. parse ("file: // storage/sdcard1/DCIM/Camera/IMG_20150622_172748.jpg"); intent. setDataAndType (uri, "image/*"); startActivity (intent) ;}}); // Button click event-open the video Button btn_video = (Button) findViewById (R. id. video); btn_video.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); // sets the Uri = uri of the current action. parse ("file: // storage/sdcard1/---"); intent. setDataAndType (uri, "video/*"); startActivity (intent) ;}}); // The registration event of the Button-view the address book Button SeeContacts_BT = (Button) findViewById (R. id. seeContacts); SeeContacts_BT.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); intent. setData (Uri. parse ("content: // contacts/people/1"); // view the first contact startActivity (intent) ;}} in the address book );}});
5. Intent. ACTION_SENDTO
String: android. intent. action. SENDTO
Purpose: send short messages.
Note: several simple applications are demonstrated here;
Example:
String number = numberET. getText (). toString (); // obtain the user input number String message = messageET. getText (). toString (); // obtain the text message if (PhoneNumberUtils. isGlobalPhoneNumber (number) {Intent intent = new Intent (); // create Intent object intent. setData (Uri. parse ("smsto:" + number); // sets the intent number to be sent. putExtra ("sms_body", message); // you can specify startActivity (intent) as the message to be sent. // you can call this operation to send Intent to Activity} else {Toast. makeText (SendToActivity. th Is, "the phone number you entered is incorrect. Please enter it again! ", Toast. LENGTH_SHORT). show ();}
(Android can send text messages in two ways: the first one is the one written above-> enable the system to send text messages;
In this way, when you click the send button, the system opens the built-in text messaging program on your mobile phone,
You have written the entered phone number and text message content on it. You can only send the text message by clicking the send button of the SMS program that comes with your mobile phone,
Just like the previous ACTION_DIAL, the dial is opened and the call is not started. Second, the system SMS interface is called by SmsManager to send messages directly;
In this way, when you click the send text message button you wrote, it is sent directly without calling other text message programs.
In addition, this method can monitor the sending status and recipient's receiving status (For details, refer to later ))
6. Intent. ACTION_BATTERY_LOW
Purpose: display low-power warning information
Note: This piece uses broadcast, so the specific implementation method is written when reviewing broadcast-related knowledge.
7. Intent. ACTION_EDIT
Purpose: edit a specific contact information in the address book.
Example:
Intent intent = new Intent (); intent. setAction (Intent. ACTION_EDIT); intent. setData (Uri. parse ("content: // contacts/people/1"); // modify the first contact startActivity (intent) in the address book );
(There are other ways to get all the contact information in the contact list-the content provider is used, and we will talk about it later)
Reference URL and materials:
1. http://blog.csdn.net/coder80/article/details/7879259
2. http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2417278.html
3. Android app development example book