Intent idioms in Android

Intent in Android is a very important class. If you don't have a special understanding of intent, please refer to explain how to use intent in Android. If you don't know much about intent filter, please refer to explain the filtering matching process of intent object and intent filter in Android.

This article focuses on some common uses of intent in Android, such as how to send text messages, send e-mail, start the camera to take photos and record videos, set the alarm, open the WiFi setting interface, and so on.

Limited to space, this paper is divided into two parts, which is the first part.

Send SMS

When sending text messages, the action we want to use is intent.action_ SendTo, and specify that its URI is smsto: protocol, which can ensure that the SMS application receives and processes our intent object instead of other applications, so as to accurately realize the purpose of sending SMS. If our action is not intent.action_ SendTo, but intent.action_ Send without specifying the URI of smsto: protocol, Android will not directly start the SMS application after receiving the intent object, but will pop up the app chooser, allowing us to choose which application to start, such as e-mail, QQ, etc. Therefore, in order to ensure the direct start of the SMS application, we should use intent.action_ SendTo and specify the URI of the smsto: protocol.

The example code is as follows:

When constructing the URI for sending SMS, smsto: protocol is preceded, followed by the mobile phone number of the other party receiving the SMS. If only smsto: is written when constructing the URI, but the following mobile phone number is not written, the intent can also successfully start the SMS application. However, in this case, after starting the SMS application, we need to manually enter the mobile phone number to receive the information. We set the key for SMS_ The extra of the body sets the content of the SMS.

It should be noted that after executing startactivity (intent), although the SMS application is started, the SMS is not sent directly. We need to click to send the message again.

Send mail

When sending mail, the action we want to use is also intent.action_ SendTo, and specify that its URI is mailto: protocol, which can ensure that the mail application receives and processes our intent object, rather than other applications, so as to accurately realize the purpose of sending mail. If our action is not intent.action_ SendTo, but intent.action_ Send without specifying the URI of mailto: protocol, Android will not directly mail the application after receiving the intent object, but will pop up the app chooser, allowing us to select which application to start, such as SMS, QQ, etc. Therefore, in order to ensure that the email application is started directly, we should use intent.action_ SendTo and specify the URI of the mailto: protocol.

The example code is as follows:

The screenshot after starting the mail application is as follows:

We set the key to intent.extra_ EMAIL、Intent.EXTRA_ CC and intent.extra_ BCC's extra sets the receiver, CC party and BCC Party of the mail in sequence, and their values are all string arrays. We set the key to intent.extra_ Set the e-mail header in extra of subject, and set the key to intent.extra_ Extra of text sets the message content. If you want to send an attachment, you can encapsulate the attachment in the form of URI, and then set the key to intent.extra_ Extra of stream sets the mail attachment.

It should be noted that after executing startactivity (intent), although the mail application is started and opened, the mail is not sent directly. We need to click the send button in the upper right corner to send the mail.

phone

To make a phone call through intent, we have two actions we can use: intent. Action_ Dial and intent.action_ Call, there are some differences between the two.

If intent.action is used_ Dial is an action of intent object. After executing startactivity (intent), the calling application will be started, and the specified mobile phone number will be entered automatically, but it will not be dialed automatically. We need to manually press the dial button to really call the other party.

If intent.action is used_ Call is the action of the intent object. After the startactivity (intent) is executed, the calling application will be started and the mobile phone number specified by us will be dialed directly. There is no need for us to press the dial button manually. However, it should be noted that the action requires the permission android.permission.call_ For phone, if the permission is not added in the androidmanifest.xml file of the application, an exception will be thrown when the code startactivity (intent) is specified, and the application crashes and exits.

The following is sample code:

In this sample code, we use intent. Action_ Call is the action of the intent object, and the following permissions are added to androidmanifest.xml:

photograph

To start the camera to take pictures through intent, we need to set the action value of intent object to mediastore.action_ IMAGE_ Action of capture. Then we use the key as mediastore.extra_ OUTPUT's extra sets the output path of the picture, and finally calls the startActivityForResult () method to start the camera application, and rewrites our onActivityResult () to learn that the camera is complete in this method.

The example code is as follows:

Let's analyze the above code snippet:

Not all Android devices can take pictures, so first we call the hassystemfeature (packagemanager. Feature_camera) method of packagemanager to judge whether the current device has the ability to take pictures at the hardware level.

Then we created an action called mediastore. Action_ IMAGE_ The intent object of the capture.

Then we call the intent. Resolveactivity (PM) method to determine whether the current device has a camera application so that we can start it. If there is no camera application, but we pass the intent object to startactivity () or startactivityforresult (), an exception will be thrown and the application will crash and exit.

We wrote a createimagefile method by ourselves. Through this method, we created a picture file on the peripheral memory card corresponding to our application. Note that this step requires write_ EXTERNAL_ The storage permission is registered in androidmanifest.xml as follows:

We use the image file generated above to generate the corresponding URI and store it in the field imageoutputuri with the type of URI in the activity. Then we execute intent.putextra (mediastore.extra_output, imageoutputuri) and use the URI as the storage path of the photos after photographing. It should be noted here that once the storage path is set, we cannot get thumbnails in onactivityresult().

Finally, we need to call the method startactivityforresult (intent, request_code_image_capture) to start the camera application to take photos, where request_ CODE_ IMAGE_ Capture is the requestcode for photographing specified by us.

We override the onactivityresult method, and trigger the execution of this method after taking photos. First, we need to determine whether the resultcode is consistent with the result_ OK is equal. Only equal indicates successful photographing. Then we judge whether requestcode is equal to request_ CODE_ IMAGE_ Capture, if equal, it indicates that it is the result returned by photographing. At this time, we can get the just taken photo through the imageoutputuri we stored earlier, and its URI string is as follows: file:///storage/sdcard0/Android/data/com.ispring.commonintents/files/Pictures/JPEG_ 20150919_ 112704_ 533002075.jpg note that if we set mediastore.extra in step 5_ If output is used as the photo output path, the intent exchanged from the camera application cannot be obtained in onactivityresult, that is, it is null, so the thumbnail cannot be obtained. Conversely, if mediastore.extra is not set in step 5_ If output is used as the photo output path, intent is not empty. You can try to execute bitmap thumbnail = intent.getparcelableextra ("data") to obtain thumbnails. If thumbnail is not empty, it indicates that thumbnails can be obtained successfully. However, some mobile phones do not generate thumbnails for the pictures taken, so the thumbnail here may also be null, so judge before using it.

Camera

The steps of starting the camera through intent are very similar to the steps of starting the camera through intent just mentioned above, with slight differences. To start camera for shooting, we need to set the value of intent to mediastore.action_ VIDEO_ The action of capture, and then we use the key as mediastore.extra_ OUTPUT's extra sets the output path of the picture, and finally calls the startActivityForResult () method to start the camera application, and rewrites our onActivityResult () to learn the camera completion in this method.

The following is sample code:

It can be seen that the above code for starting camera shooting is almost the same as the code for taking photos. See the description of the shooting code for specific explanation. In this sample code, we use mediastore.extra_ Output sets the storage path of video. When taking photos, we also set the output path of photos through it, but there is a slight difference between the two:

The above is about the common intention idioms in Android. I hope it will be helpful to everyone's study.

Attached source code: intent idioms in Android

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>