In the development of Android application, it is unavoidable to invoke the message class application on the mobile phone to realize the function of mail sending, which is usually realized by invoking the intent of the system. See many e-mail sent on the internet is called ACTION to Android.content.Intent.ACTION_SEND Intent to achieve, below we see how this approach to achieve the effect.
"Use Intent.action_send Mode"
Specific UI build I don't say, very easy, directly look at the core code to send the line:
- string[] email = {"3802**[email protected]"}; //need to be aware that email must be passed in as an array
- Intent Intent = new Intent (intent.action_send);
- Intent.settype ("message/rfc822"); //Set message format
- Intent.putextra (Intent.extra_email, EMAIL); //Receiving person
- Intent.putextra (intent.extra_cc, email); //CC person
- Intent.putextra (Intent.extra_subject, "This is the subject part of the message"); //Theme
- Intent.putextra (Intent.extra_text, "This is the body part of the message"); //Body
- StartActivity (Intent.createchooser (Intent, "Please select Mail class application");
The effect of the above code as shown, will pull up more than the Mail class application, this is very pit dad thing, the user experience is quite poor.
As can be seen from the above results, Action_send is not the preferred solution, a better solution is to be able to filter non-mail applications, only to identify the mail class application. This can be done by using a different action.
"Use Intent.action_sendto Mode"
The core code is as follows:
- You must explicitly use the mailto prefix to decorate your e-mail address if you use
- Intent.putextra (Intent.extra_email, EMAIL), results will not match any application
- Uri uri = uri.parse ("Mailto:3802**[email protected]");
- string[] email = {"3802**[email protected]"};
- Intent Intent = new Intent (Intent.action_sendto, URI);
- Intent.putextra (intent.extra_cc, email); //CC person
- Intent.putextra (Intent.extra_subject, "This is the subject part of the message"); //Theme
- Intent.putextra (Intent.extra_text, "This is the body part of the message"); //Body
- StartActivity (Intent.createchooser (Intent, "Please select Mail class application");
As shown below:
When there is only one matching message class app on the phone, the system will not pop up the chooseractivity for the user to pick one from the candidate application and jump directly to the matching application. At this point, the logcat of Eclipse can be found with the following exception information:
However, when there are more than one matching app in the system, the chooseractivity will pop up to let the user select an app to send the message, and the previous exception will not appear when viewing logcat. Why is this?
"Chooseractivity Bug"
As can be seen from the exception information, the code in a place registered Intentreceiver, but there is no code to de-register. The specific reason is a bug of Android source code, but does not affect the normal function, so it can be ignored. Want to know the details of the reasons can be self-view Android source code. It is the answer of StackOverflow above, can refer to it.
Url:
http://stackoverflow.com/questions/10068954/ why-does-intent-createchooser-need-a-broadcastreceiver-and-how-to-implement/10290486#10290486
Demo Source Address: http://download.csdn.net/detail/ace1985/4695230
The correct implementation method of Android call system Mail class application