[Android] Getting Started: Intent introduction and common usage Summary

Source: Internet
Author: User
Tags rfc822

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 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
[Java]
// Call mobile customer service 10086
Uri uri = Uri. parse ("tel: 10086 ");
Intent intent = new Intent (Intent. ACTION_DIAL, uri );
StartActivity (intent );

// Call mobile customer service 10086
Uri uri = Uri. parse ("tel: 10086 ");
Intent intent = new Intent (Intent. ACTION_DIAL, uri );
StartActivity (intent );


2. send SMS or MMS messages
[Java]
// Send a "Hello" text message to 10086
Uri uri = Uri. parse ("smsto: 10086 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "Hello ");
StartActivity (intent );
// Send MMS (equivalent to sending an SMS with an attachment)
Intent 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 );

// Send a "Hello" text message to 10086
Uri uri = Uri. parse ("smsto: 10086 ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
Intent. putExtra ("sms_body", "Hello ");
StartActivity (intent );
// Send MMS (equivalent to sending an SMS with an attachment)
Intent 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
[Java]
// Open the Google Homepage
Uri uri = Uri. parse ("http://www.google.com ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );

// Open the Google Homepage
Uri uri = Uri. parse ("http://www.google.com ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );


4. send an email
[Java]
// Send an email to the someone@domain.com
Uri uri = Uri. parse ("mailto: someone@domain.com ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
StartActivity (intent );
// Send an email with the content "Hello" to the someone@domain.com
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 emails to multiple users
Intent 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 );

// Send an email to the someone@domain.com
Uri uri = Uri. parse ("mailto: someone@domain.com ");
Intent intent = new Intent (Intent. ACTION_SENDTO, uri );
StartActivity (intent );
// Send an email with the content "Hello" to the someone@domain.com
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 emails to multiple users
Intent 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
[Java]
/Open Google map China's Beijing location (north latitude 39.9, east longitude 116.3)
Uri uri = Uri. parse ("geo: 39.9, 116.3 ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );
// Route Planning: from a certain place in Beijing (north latitude 39.9, east longitude 116.3) to a certain place in Shanghai (north latitude 31.2, east longitude 121.4)
Uri uri = Uri. parse ("http://maps.google.com/maps? F = d & saddr = 39.9 116.3 & daddr = 31.2 121.4 ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );

// Open Google map's location in Beijing, China (latitude 39.9, longitude 116.3)
Uri uri = Uri. parse ("geo: 39.9, 116.3 ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );
// Route Planning: from a certain place in Beijing (north latitude 39.9, east longitude 116.3) to a certain place in Shanghai (north latitude 31.2, east longitude 121.4)
Uri uri = Uri. parse ("http://maps.google.com/maps? F = d & saddr = 39.9 116.3 & daddr = 31.2 121.4 ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );


6. Play multimedia
[Java]
Intent intent = new Intent (Intent. ACTION_VIEW );
Uri uri = Uri. parse ("file: // sdcard/foodie ");
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 );

Intent intent = new Intent (Intent. ACTION_VIEW );
Uri uri = Uri. parse ("file: // sdcard/foodie ");
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
[Java]
// Open the camera program
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, 0 );
// Retrieve Photo Data
Bundle extras = intent. getExtras ();
Bitmap bitmap = (Bitmap) extras. get ("data ");

// Open the camera program
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, 0 );
// Retrieve Photo Data
Bundle extras = intent. getExtras ();
Bitmap bitmap = (Bitmap) extras. get ("data ");


8. Get and cut the image
[Java]
// Obtain and cut the image
Intent intent = new Intent (Intent. ACTION_GET_CONTENT );
Intent. setType ("image /*");
Intent. putExtra ("crop", "true"); // enable cut
Intent. putExtra ("aspectX", 1); // the aspect ratio of the cut is.
Intent. 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 path
Intent. putExtra ("outputFormat", "JPEG"); // return format
StartActivityForResult (intent, 0 );
// Cut a specific image
Intent 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.
Intent. 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 );

// Obtain and cut the image
Intent intent = new Intent (Intent. ACTION_GET_CONTENT );
Intent. setType ("image /*");
Intent. putExtra ("crop", "true"); // enable cut
Intent. putExtra ("aspectX", 1); // the aspect ratio of the cut is.
Intent. 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 path
Intent. putExtra ("outputFormat", "JPEG"); // return format
StartActivityForResult (intent, 0 );
// Cut a specific image
Intent 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.
Intent. 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
[Java]
// Open Google Market to go to the detailed page of the program.
Uri uri = Uri. parse ("market: // details? Id = "+" com. demo. app ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );

// Open Google Market to go to the detailed page of the program.
Uri uri = Uri. parse ("market: // details? Id = "+" com. demo. app ");
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );


10. Install and uninstall the program
[Java]
Uri uri = Uri. fromParts ("package", "com. demo. app", null );
Intent intent = new Intent (Intent. ACTION_DELETE, uri );
StartActivity (intent );

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.
[Java]
// Enter the wireless network settings page (for other settings, refer to the following code)
Intent intent = new Intent (android. provider. Settings. ACTION_WIRELESS_SETTINGS );
StartActivityForResult (intent, 0 );

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.