Académique Documents
Professionnel Documents
Culture Documents
HOME
JAVA
PHP
Python
Ruby
Perl
HTML
CSS
Javascript
MySQL
C++
UNIX
MORE...
Previous Page
Android Basics
Android Home Android Overview Android Environment Setup Android Architecture Application Components Hello World Example Android Resources Android Activities Android Services Android Broadcast Receivers Android Content Providers Android Fragments Android Intents/Filters
You have learnt Android Intent, which is an object carrying an intent ie. message from one to another component with-in the application or outside the application.
As such you do not need to develop your email client from scratch because they are alrea like Gmail and K9mail. But you will need to send email from your Android application, wh have to write an Activity that needs to launch an email client and sends an email using y device. For this purpose, your Activity will send an ACTION_SEND along with appropriate the Android Intent Resolver. The specified chooser gives the proper interface for the user to send your email data. Following section explains different parts of our Intent object required to send an email.
You will use ACTION_SEND action to launch an email client installed on your Android devic is simple syntax to create an intent with ACTION_SEND action I n t e n te m a i l I n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ S E N D ) ;
To send an email you need to specify mailto: as URI using setData() method and data ty text/plain using setType() method as follows: e m a i l I n t e n t . s e t D a t a ( U r i . p a r s e ( " m a i l t o : " ) ) ; e m a i l I n t e n t . s e t T y p e ( " t e x t / p l a i n " ) ;
Android has built-in support to add TO, SUBJECT, CC, TEXT etc. fields which can be atta intent before sending the intent to a target email client. You can use following extra fields in S.N. Extra Data & Description 1 2 3 EXTRA_BCC A String[] holding e-mail addresses that should be blind carbon copied. EXTRA_CC A String[] holding e-mail addresses that should be carbon copied. EXTRA_EMAIL A String[] holding e-mail addresses that should be delivered to.
EXTRA_HTML_TEXT A constant String that is associated with the Intent, used with ACTION_SEND to sup alternative to EXTRA_TEXT as HTML formatted text. EXTRA_SUBJECT A constant string holding the desired subject line of a message.
1/7
http://www.tutorialspoint.com/android/android_sending_email.htm
5/2/2014
EXTRA_TEXT A constant CharSequence that is associated with the Intent, used with ACTION_SEN supply the literal data to be sent.
EXTRA_TITLE A CharSequence dialog title to provide to the user when used with a ACTION_CHOO
Here is an example showing you how to assign extra data to your intent:
Example
Following example shows you in practical how to use Intent object to launch Email clien Email to the given recipients.
To experiment with this example, you will need actual Mobile device equipped with lat Android OS, otherwise you will have to struggle with emulator which may not work. Se will need to have an Email client like GMail or K9mail installed on your device. Step Description
You will use Eclipse IDE to create an Android application and name it as SendEma under a package com.example.sendemaildemo. While creating this project, make Target SDK and Compile With at the latest version of Android SDK to use higher lev APIs.
Modify src/MainActivity.java file and add required code to take care of sending emai
Modify layout XML file res/layout/activity_main.xml add any GUI component if require adding a simple button to launch Email Client. Modify res/values/strings.xml to define required constant values Modify AndroidManifest.xml as shown below Run the application to launch Android emulator and verify the result of the changes aplication. modified main
act
@ O v e r r i d e p r o t e c t e dv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ; s e t C o n t e n t V i e w ( R . l a y o u t . a c t i v i t y _ m a i n ) ;
2/7
http://www.tutorialspoint.com/android/android_sending_email.htm
5/2/2014
Android RSS Reader Android Screen Cast Android SDK Manager Android Sensors Android Session Management Android Shared Preferences Android SIP Protocol Android Spelling Checker Android SQLite Database Android Support Library Android Testing Android Text to Speech Android TextureView Android Twitter Integration Android UI Deign Android UI Patterns Android UI Testing Android WebView Layout Android Wi-Fi Android Widgets Android XML Parsers
B u t t o ns t a r t B t n=( B u t t o n )f i n d V i e w B y I d ( R . i d . s e n d E m a i l ) ; s t a r t B t n . s e t O n C l i c k L i s t e n e r ( n e wV i e w . O n C l i c k L i s t e n e r ( ){ p u b l i cv o i do n C l i c k ( V i e wv i e w ){ s e n d E m a i l ( ) ; } } ) ; } p r o t e c t e dv o i ds e n d E m a i l ( ){ L o g . i ( " S e n de m a i l " ," " ) ; S t r i n g [ ]T O={ " a m r o o d . a d m i n @ g m a i l . c o m " } ; S t r i n g [ ]C C={ " m c m o h d @ g m a i l . c o m " } ; I n t e n te m a i l I n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ S E N D ) ; e m a i l I n t e n t . s e t D a t a ( U r i . p a r s e ( " m a i l t o : " ) ) ; e m a i l I n t e n t . s e t T y p e ( " t e x t / p l a i n " ) ;
} @ O v e r r i d e p u b l i cb o o l e a no n C r e a t e O p t i o n s M e n u ( M e n um e n u ){ / /I n f l a t et h em e n u ;t h i sa d d si t e m st ot h ea c t i o nb a ri fi ti s g e t M e n u I n f l a t e r ( ) . i n f l a t e ( R . m e n u . m a i n ,m e n u ) ; r e t u r nt r u e ; }
Selected Reading
Developer's Best Practices Effective Resume Writing Computer Glossary Who is Who
< L i n e a r L a y o u tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " f i l l _ p a r e n t " a n d r o i d : o r i e n t a t i o n = " v e r t i c a l "> < B u t t o na n d r o i d : i d = " @ + i d / s e n d E m a i l " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t " a n d r o i d : t e x t = " @ s t r i n g / c o m p o s e _ e m a i l " / > < / L i n e a r L a y o u t > Following will be the content of res/values/strings.xml to define two new constants: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < r e s o u r c e s > < s t r i n gn a m e = " a p p _ n a m e " > S e n d E m a i l D e m o < / s t r i n g > < s t r i n gn a m e = " h e l l o _ w o r l d " > H e l l ow o r l d ! < / s t r i n g > < s t r i n gn a m e = " a c t i o n _ s e t t i n g s " > S e t t i n g s < / s t r i n g >
http://www.tutorialspoint.com/android/android_sending_email.htm
3/7
5/2/2014
< s t r i n gn a m e = " c o m p o s e _ e m a i l " > C o m p o s eE m a i l < / s t r i n g > < / r e s o u r c e s > Following is the default content of AndroidManifest.xml: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < m a n i f e s tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " p a c k a g e = " c o m . e x a m p l e . s e n d e m a i l d e m o " a n d r o i d : v e r s i o n C o d e = " 1 " a n d r o i d : v e r s i o n N a m e = " 1 . 0 "> < u s e s s d k a n d r o i d : m i n S d k V e r s i o n = " 8 " a n d r o i d : t a r g e t S d k V e r s i o n = " 1 7 "/ > < a p p l i c a t i o n a n d r o i d : a l l o w B a c k u p = " t r u e " a n d r o i d : i c o n = " @ d r a w a b l e / i c _ l a u n c h e r " a n d r o i d : l a b e l = " @ s t r i n g / a p p _ n a m e " a n d r o i d : t h e m e = " @ s t y l e / A p p T h e m e "> < a c t i v i t y a n d r o i d : n a m e = " c o m . e x a m p l e . s e n d e m a i l d e m o . M a i n A c t i v i t y " a n d r o i d : l a b e l = " @ s t r i n g / a p p _ n a m e "> < i n t e n t f i l t e r > < a c t i o na n d r o i d : n a m e = " a n d r o i d . i n t e n t . a c t i o n . M A I N "
< c a t e g o r ya n d r o i d : n a m e = " a n d r o i d . i n t e n t . c a t e g o r y . L A U N C H < / i n t e n t f i l t e r > < / a c t i v i t y > < / a p p l i c a t i o n > < / m a n i f e s t >
Let's try to run your SendEmailDemo application. I assume you have connected your ac Mobile device with your computer. To run the app from Eclipse, open one of your project's and click Run
icon from the toolbar. Before starting your application, Eclipse will disp
window to select an option where you want to run your Android application.
http://www.tutorialspoint.com/android/android_sending_email.htm
4/7
5/2/2014
Select your mobile device as an option and then check your mobile device which will disp screen:
Now use Compose Email button to list down all the installed email clients. From the choose one of email clients to send your email. I'm going to use Gmail client to send my will have all the provided defaults fields available as shown below. Here From: will be defa you have registered for your Android device.
http://www.tutorialspoint.com/android/android_sending_email.htm
5/7
5/2/2014
You can modify either of the given default fields and finally use send email button (mark rectangle) to send your email to the mentioned recipients.
Previous Page
Advertisements
Print Version
http://www.tutorialspoint.com/android/android_sending_email.htm
6/7
5/2/2014
ASP.NET | jQuery | AJAX | ANT | JSP | Servlets | log4j | iBATIS | Hibernate | JDBC | Struts |
http://www.tutorialspoint.com/android/android_sending_email.htm
7/7