Email Intent Demo

This example will show you how to open email composer through your application.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it EmailIntentDemo.

2.) You will see some default code into your main.xml, strings.xml and android manifest file.

3.) Now add 1 buttons into your main.xml or write following into main.xml file:

 

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<button> android:layout_width="wrap_content" android:layout_height="wrap_content"></button>

 

 

4.) Your launcher activity EmailIntentDemo will have one default functions OnCreate().
5.) You must have to configure any email onto your device or simulator to get the output on this demo.
6.) To configure email goto applications and select “Email”, now follow the instructions given and configure your email onto device or simulator.

Steps:

1.) Create a project named EmailIntentDemo and set the information as stated in the image.

Build Target: Android 1.6
Application Name: EmailIntentDemo
Package Name: com.example. EmailIntentDemo
Activity Name: EmailIntentDemo
Min SDK Version: 4

2.) Open EmailIntentDemo.java file and write following code there:

 

package com.example.EmailIntentDemo;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class EmailIntentDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button shareButton = (Button) findViewById(R.id.button1);
shareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Email Intent Example");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email is generated using EmailIntent Demo application to sow how we can directly compose a mail from our application.");
final PackageManager pm = getPackageManager();
@SuppressWarnings("static-access")
final List matches = pm.queryIntentActivities(emailIntent, pm.MATCH_DEFAULT_ONLY);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
{
if (info.activityInfo.name.toLowerCase().contains("mail"))
best = info;
}
if (best != null)
{
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
startActivity(emailIntent);
}
}
});

}
}

 

 

3.) Compile and build the project.

4.) Run on 1.6 simulator for the output.

Leave a Comment:

3 comments
rajesh says October 12, 2011

how to share SQLite database between multiple activity
give me Example demo

Reply
nope says July 25, 2012

doesnt work. multiple errors and wont compile

Reply
Gopal kumar says June 3, 2013

Good example and it helped ma a lot……how we will attach image with it.

Reply
Add Your Reply