Email Intent Demo In Android

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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
        <Button android:text="Share" android:id="@+id/button1"

                android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
</LinearLayout>
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 = newIntent(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<ResolveInfo> 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.

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.