Intent.
It is mainly used for interaction and communication between various components of an application, and also for interaction between activities/services within the application.
For example, you can call an external program in an application, call the call directly, or switch between activities in the application.
It can be said that it is the essence of loose coupling of the android architecture.
Intent describes the actions, actions involving data, and additional data of an application;
Android finds the corresponding component based on the description of the intent, passes the intent to the called component, and calls the component.
Intent serves as a media intermediary, providing information about component calls to each other to decouple callers from callers.
For example:
In an application maintained by a contact,
When we click a contact on a contact list screen (assuming the corresponding activity is listactivity,
You want to jump out of the contact's detail screen (assuming the corresponding activity is detailactivity)
To achieve this, listactivity needs to construct an intent,
This intent is used to tell the system that we want to perform the "View" action. The corresponding object of this action is "a contact ",
Call startactivity (intent) to pass in the constructed intent,
The system will find the activity that meets the intent requirement in manifest according to the description in this intent,
The system will call the activity found, that is, detailactivity, and finally pass in the intent,
Detailactivity performs the corresponding operation based on the description in the intent.
Below are some common intent usage summaries:
1. Call the dialup Program
// Call mobile customer service 10086 URI uri = URI. parse ("Tel: 10086"); intent = new intent (intent. action_dial, Uri); startactivity (intent );
2. send SMS or MMS messages
// Send "hello" text message uri = URI to 10086. parse ("Maid: 10086"); intent = new intent (intent. action_sendto, Uri); intent. putextra ("sms_body", "hello"); startactivity (intent); // send MMS (equivalent to sending SMS messages with attachments) intent = new intent (intent. action_send); intent. putextra ("sms_body", "hello"); Uri uri = Uri. parse ("content: // media/external/images/Media/23"); intent. putextra (intent. extra_stream, Uri); intent. settype ("image/PNG"); startactivity (intent );
3. Open the webpage through a browser
// Open the Google homepage URI uri = URI. parse ("http://www.google.com"); intent = new intent (intent. action_view, Uri); startactivity (intent );
4. send an email
// Send an email URI uri = URI to the someone@domain.com. parse ("mailto: someone@domain.com"); intent = new intent (intent. action_sendto, Uri); startactivity (intent); // intent = new intent (intent. action_send); intent. putextra (intent. extra_email, "someone@domain.com"); intent. putextra (intent. extra_subject, "subject"); intent. putextra (intent. extra_text, "hello"); intent. settype ("text/plain"); startactivity (intent); // send intent = new intent (intent. action_send); string [] TOS = {"1@abc.com", "2@abc.com"}; // recipient string [] CCS = {"3@abc.com", "4@abc.com "}; // CC string [] BCCs = {"5@abc.com", "6@abc.com"}; // BCC intent. putextra (intent. extra_email, TOS); intent. putextra (intent. extra_cc, CCS); intent. putextra (intent. extra_bcc, BCCs); intent. putextra (intent. extra_subject, "subject"); intent. putextra (intent. extra_text, "hello"); intent. settype ("message/rfc822"); startactivity (intent );
5. Display map and Path Planning
// Open Google map China's Beijing location (latitude 39.9, longitude 116.3) URI uri = Uri. parse ("Geo: 39.9, 116.3"); intent = new intent (intent. action_view, Uri); startactivity (intent); // Path Planning: URI uri = URI from a certain place in Beijing (latitude 39.9, longitude 116.3) to a place in Shanghai (latitude 31.2, longitude 121.4. parse ("http://maps.google.com/maps? F = D & saddr = 39.9 116.3 & daddr = 31.2 121.4 "); intent = new intent (intent. action_view, Uri); startactivity (intent );
6. Play multimedia
Intent intent = new Intent(Intent.ACTION_VIEW);Uri uri = Uri.parse("file:///sdcard/foo.mp3");intent.setDataAndType(uri, "audio/mp3");startActivity(intent);Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");Intent intent = new Intent(Intent.ACTION_VIEW, uri);startActivity(intent);
7. Take a photo
// Open the camera program intent = new intent (mediastore. action_image_capture); startactivityforresult (intent, 0); // bundle extras = intent. getextras (); Bitmap bitmap = (Bitmap) extras. get ("data ");
8. Get and cut the image
// Obtain and cut the image intent = new intent (intent. action_get_content); intent. settype ("image/*"); intent. putextra ("crop", "true"); // enable intent cut. putextra ("aspectx", 1); // the aspect ratio of the cut is 1: 2intent. putextra ("aspecty", 2); intent. putextra ("outputx", 20); // Save the width and height of the image intent. putextra ("outputy", 40); intent. putextra ("output", Uri. fromfile (new file ("/mnt/sdcard/Temp"); // Save the path intent. putextra ("outputformat", "Jpeg"); // return format startactivityforresult (intent, 0); // cut the intent = new intent ("com. android. camera. action. crop "); intent. setclassname ("com. android. camera "," com. android. camera. cropimage "); intent. setdata (URI. fromfile (new file ("/mnt/sdcard/Temp"); intent. putextra ("outputx", 1); // the aspect ratio of the cut is 1: 2intent. putextra ("outputy", 2); intent. putextra ("aspectx", 20); // Save the width and height of the image intent. putextra ("aspecty", 40); intent. putextra ("scale", true); intent. putextra ("nofacedetection", true); intent. putextra ("output", Uri. parse ("file: // MNT/sdcard/Temp"); startactivityforresult (intent, 0 );
9. Open google Market
// Open google market and go to the detailed page of the program. Uri uri = URI. parse ("Market: // details? Id = "+" com. Demo. app "); intent = new intent (intent. action_view, Uri); startactivity (intent );
10. Install and uninstall the program
Uri uri = Uri.fromParts("package", "com.demo.app", null);Intent intent = new Intent(Intent.ACTION_DELETE, uri);startActivity(intent);
11. Go to the settings page.
// Go to the wireless network settings page (others can refer to the opposite). Intent intent = new intent (Android. provider. settings. action_wireless_settings); startactivityforresult (intent, 0 );