Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: MIT License 

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Context;
import android.content.Intent;

import android.net.Uri;

public class Main {
    public static void composeEmail(Context context, String email) {
        viewURL(context, "mailto:" + email);
    }

    public static void composeEmail(Context context, String email, String subject, String body) {
        try {
            String url = "mailto:";
            if (email != null) {
                url += email;
            }
            url += "?";

            if (subject != null) {
                String subjectEncoded = URLEncoder.encode(subject, "UTF-8");
                subjectEncoded = subjectEncoded.replace("+", "%20");
                url += "subject=" + subjectEncoded;
            }

            if (body != null) {
                if (subject != null) {
                    url += "&";
                }
                String bodyEncoded = URLEncoder.encode(body, "UTF-8");
                bodyEncoded = bodyEncoded.replace("+", "%20");
                url += "body=" + bodyEncoded;
            }

            viewURL(context, url);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void composeEmail(Context context, String email, String subject) {
        composeEmail(context, email, subject, null);
    }

    public static void viewURL(Context context, String url) {
        Intent viewURLIntent = new Intent(Intent.ACTION_VIEW);
        viewURLIntent.setData(Uri.parse(url));
        context.startActivity(viewURLIntent);
    }
}