Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.04 MB, 688 trang )
Problem
Send an Email containing text or images from a View. The data to be emailed is passed
as a parameter using an Intent.
Solution
File: AndroidManifest.xml
Example 1-9.
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.examples"
android:versionCode="1"
android:versionName="1.0">
android:icon="@drawable/icon"
android:label="@string/app_name">
android:name=".Main"
android:label="@string/app_name">
android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />
android:name="android.permission.INTERNET" />
android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.ACCESS_COARSE_LOCATION">
android:name="android.permission.ACCESS_FINE_LOCATION">
File: main.xml
Example 1-10.
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
36 | Chapter 1: Getting Started
www.it-ebooks.info
android:id="@+id/emailButton"
android:text="Email Text!"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/text_to_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/my_text" />
File: strings.xml
Example 1-11.
<?xml version="1.0" encoding="utf-8"?>
name="hello">Hello World, Main!
name="app_name">EmailAndroid
name="my_text">
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when
an unknown printer took a galley of type and scrambled it to make a type
specimen book. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum."
File: Main.java
Example 1-12.
import
import
import
import
import
import
android.app.Activity;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private Button emailButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
1.9 Email Text From a View | 37
www.it-ebooks.info
{
super.onCreate(savedInstanceState);
// Set the View Layer
setContentView(R.layout.main);
// Get referenc to Email Button
this.emailButton = (Button) this.findViewById(R.id.emailButton);
// Sets the Event Listener onClick
this.emailButton.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
if (view == this.emailButton)
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_TITLE, "My Title");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
}
}
}
// Obtain refenerenc to String and pass it to Intent
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.my_text));
startActivity(emailIntent);
Discussion
1. Modify AndroidManifest.xml to allow for internet connection allowing email to be
sent. 2. Create the the visual presentation layer with Email Button which the user clicks.
3. Attach a OnClickListener to allow the email to be sent when the user clicks the Email
button.
Source Download URL
The source code for this example may be downloaded from this URL: http://www.filefactory.com/file/b43debh/n/EmailAndroid.zip
1.10 Sending an email with attachments
Marco Dinacci
Problem
You want to send an e-mail with attachments.
38 | Chapter 1: Getting Started
www.it-ebooks.info
Solution
We're going to create an Intent, add extended data to specify the file we want to include
and start a new activity to allow the user to send the e-mail.
Discussion
The easiest way to send an e-mail is to create an Intent of type ACTION_SEND.
Example 1-13.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test single attachment");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
intent.putExtra(Intent.EXTRA_TEXT, "Mail with an attachment");
To attach a single file, we add some extended data to our intent:
Example 1-14.
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/plain");
The MIME type can always be set as text/plain but you may want to be more specific
so applications parsing your message will work properly. For instance if you're including a JPEG image you should write image/jpeg.
To send an e-mail with multiple attachment the procedure is slightly different:
Example 1-15.
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple attachments");
intent.putExtra(Intent.EXTRA_TEXT, "Mail with multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
First, we need to use Intent.ACTION_SEND_MULTIPLE, which is available since Android
1.6. Second, we need to create an ArrayList with the URIs of the files we want to attach
to the mail and call putParcelableArrayListExtra.
If sending different type of files you may want to use multipart/mixed as MIME type.
Finally, in both cases, you can start a new Activity with the following code:
Example 1-16.
startActivity(Intent.createChooser(intent, "Send mail"));
1.10 Sending an email with attachments | 39
www.it-ebooks.info