The correct implementation of Android call system email applications

Source: Internet
Author: User

In the development of Android applications, in many cases, it is inevitable to call Mail Applications on mobile phones to implement the mail sending function, which is generally achieved by calling the existing intent of the system. We can see that many emails sent on the Internet are implemented by calling the intent of Android. content. Intent. action_send. The following describes how this method works.


【Use intent. action_send mode]

I won't talk about the specific UI building. It's easy. Let's take a look at the core of sending.CodeJust do:

String [] email = {"3802 ** 92@qq.com"}; // you must specify intent = new intent (intent. action_send); intent. settype ("message/rfc822"); // sets the mail format intent. putextra (intent. extra_email, email); // recipient intent. putextra (intent. extra_cc, email); // CC to intent. putextra (intent. extra_subject, "this is the subject part of the email"); // The topic intent. putextra (intent. extra_text, "this is the body of the email"); // The Body startactivity (intent. createchooser (intent, "select mail application "));

As shown in the code above, more than email applications will be pulled. This is a very bad thing, and the user experience is quite poor.

The above results show that action_send is not the preferred solution. A better solution is to filter non-Mail applications and only identify Mail applications. This can be achieved by using another action.

 

【Use intent. action_sendto mode]

The core code is as follows:

 
// You must use the mailto prefix to modify the email address. putextra (intent. extra_email, email), the result will not match any application uri = Uri. parse ("mailto: 3802 ** 92@qq.com"); string [] email = {"3802 ** 92@qq.com"}; intent = new intent (intent. action_sendto, Uri); intent. putextra (intent. extra_cc, email); // CC to intent. putextra (intent. extra_subject, "this is the subject part of the email"); // The topic intent. putextra (intent. extra_text, "this is the body of the email"); // The Body startactivity (intent. createchooser (intent, "select mail application "));

As follows:

When there is only one matching email application on the mobile phone, the system will not pop up chooseractivity for the user to select one from the candidate application, but directly jump to the matching application. Check the logcat of eclipse to find the following exception:


However, when more than one matching application exists in the system, a chooseractivity will pop up asking the user to select an application to send an email. Then, check that logcat does not encounter any previous exception. Why?

[Chooseractivity bug]

From the exception information, we can see that the code registers intentreceiver somewhere, but there is no code to unregister it. The specific cause is a bug in the android source code, but it does not affect normal functions, so you can ignore it. For details, you can view the android source code. Is the answer above stackoverflow. For more information, see.


URL:

Http://stackoverflow.com/questions/10068954/why-does-intent-createchooser-need-a-broadcastreceiver-and-how-to-implement/10290486#10290486

Demo Source Code address:Http://download.csdn.net/detail/ace1985/4695230

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.